Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Convert UTC Date To Local time Zone in MySql Select Query

I am using this Where Condition in One Of my query with MySql Database.My Problem is that i have one displaytime column in my table but that table column shows the data in UTC Time.and i want to convert that displaytime column in the Local Time Zone.so how can i provide this facility from query itself.

I have goggled the things and by that i knew that something like SELECT CONVERT_TZ() will work for that.but its not working for me.

Here is my query in which i need to convert displaytime to local time zone...so can anyone please guide me?

WHERE displaytime >= '2012-12-01 00:00:00'
  AND displaytime <='2013-02-22 23:59:59'
  AND ct.organizationId IN (
    SELECT t.organizationId
      FROM organization_ AS t
      JOIN organization_ AS p ON t.treePath LIKE CONCAT(p.treePath, '%')
     WHERE p.organizationId = 10707

enter image description here SAmple DAta

enter image description here

like image 245
User 1531343 Avatar asked Feb 22 '13 05:02

User 1531343


People also ask

How do I convert UTC to time in MySQL?

Here's an example to convert EST to UTC timezone by specifying time zone names instead of offset values. mysql> select convert_tz('2020-09-17 03:00:00','US/Eastern','UTC'); Hopefully, now you can convert datetime to UTC in MySQL. Ubiq makes it easy to visualize data in minutes, and monitor in real-time dashboards.

How do you convert UTC to local time?

Add the local time offset to the UTC time. For example, if your local time offset is -5:00, and if the UTC time is shown as 11:00, add -5 to 11. The time setting when adjusted for offset is 06:00 (6:00 A.M.).

How do I change timezone in MySQL?

MySQL CONVERT_TZ() function In MySQL the CONVERT_TZ() returns a resulting value after converting a datetime value from a time zone specified as the second argument to the time zone specified as the third argument. This function returns NULL when the arguments are invalid.

How do I convert UTC to CST in SQL?

If you only need to convert from UTC to CST. You can simply use DATEADD(hour, -6, Timestamp) in your query.


3 Answers

SELECT CONVERT_TZ() will work for that.but its not working for me.

Why, what error do you get?

SELECT CONVERT_TZ(displaytime,'GMT','MET'); 

should work if your column type is timestamp, or date

http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_convert-tz

Test how this works:

SELECT CONVERT_TZ(a_ad_display.displaytime,'+00:00','+04:00'); 

Check your timezone-table

SELECT * FROM mysql.time_zone; SELECT * FROM mysql.time_zone_name; 

http://dev.mysql.com/doc/refman/5.5/en/time-zone-support.html

If those tables are empty, you have not initialized your timezone tables. According to link above you can use mysql_tzinfo_to_sql program to load the Time Zone Tables. Please try this

shell> mysql_tzinfo_to_sql /usr/share/zoneinfo 

or if not working read more: http://dev.mysql.com/doc/refman/5.5/en/mysql-tzinfo-to-sql.html

like image 51
iiro Avatar answered Sep 28 '22 06:09

iiro


In my case, where the timezones are not available on the server, this works great:

SELECT CONVERT_TZ(`date_field`,'+00:00',@@global.time_zone) FROM `table`

Note: global.time_zone uses the server timezone. You have to make sure, that it has the desired timezone!

like image 44
Felix Geenen Avatar answered Sep 28 '22 06:09

Felix Geenen


 select convert_tz(now(),@@session.time_zone,'+05:30')

replace '+05:30' with desired timezone. see here - https://stackoverflow.com/a/3984412/2359994

to format into desired time format, eg:

 select DATE_FORMAT(convert_tz(now(),@@session.time_zone,'+05:30') ,'%b %d %Y %h:%i:%s %p') 

you will get similar to this -> Dec 17 2014 10:39:56 AM

like image 39
Som Avatar answered Sep 28 '22 06:09

Som