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

4

65

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

Ellsworthellwood answered 22/2, 2013 at 5:56 Comment(3)
Looks like your mysql timezone table is empty. You have to run mysql_tzinfo_to_sql command for CONVERT_TZ() to work correctly.Scruggs
where we can get mysql_tzinfo_to_sql command?Seldom
If you have the offset, you could use this function DATE_ADD(displaytime, INTERVAL {+-YOUR_OFFSET_IN_MINUTES} MINUTE). There is no need to use IF/ELSE statement for DATE_SUB, use always DATE_ADD, sub or add will apply depending on the offcet value which could be positive or negative.Typify
M
139

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

Monteith answered 22/2, 2013 at 6:20 Comment(12)
its giving me null value SELECT CONVERT_TZ(a_ad_display.displaytime,'GMT','MET') as date_ from a_ad_display; My Column is Type of DATETIMEEllsworthellwood
Can you post what SHOW CREATE TABLE a_ad_display\G; will displayMonteith
That gives no help. Please give your table structure and sample data.Monteith
In My table Structure that is one column for which i want to convert Local DAte its type is [DATETIME] ..please see my updated question for sample dataEllsworthellwood
If that is sample data then you get nulls because your column values are null. Thus, I think that is sample result, not sample data? Anyway, I edited my answer.Monteith
So i cant use GMT OR UTC IN SELECT CONVERT_TZ(displaytime,'GMT','MET');?Ellsworthellwood
did you check your timezone table ?Monteith
where should be the timezone table?i done have time zone table?Ellsworthellwood
sorry, my mistake: select * From mysql.time_zone;Monteith
YEs, you have to load your timezone info into db. Check my answer.Monteith
Yo la utilizo así: SELECT CREADO AS ORIGINAL, CONVERT_TZ(CREADO,'GMT','America/Mexico_City'), COMMENTS FROM bd_mybd.tblcomments; |Cementation
@PedroCastillo sori, en ymmärrä mitä kirjotat.Monteith
E
31

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!

Easily answered 30/6, 2014 at 11:5 Comment(1)
I am getting 10-15 seconds more in UTC timestamp.Copse
A
17
 select convert_tz(now(),@@session.time_zone,'+05:30')

replace '+05:30' with desired timezone. see here - https://mcmap.net/q/41940/-how-do-i-get-the-current-time-zone-of-mysql

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

Alpinist answered 3/11, 2014 at 10:6 Comment(0)
M
0

Using 3-character timezones like "CST" and "EST" work inconsistently (my mysql.time_zone_name has "EST" but not "CST", for instance). To see what's available:

> SELECT * FROM mysql.time_zone_name where name like 'us/%';
+-------------------+--------------+
| Name              | Time_zone_id |
|-------------------+--------------|
| US/Alaska         | 580          |
| US/Aleutian       | 581          |
| US/Arizona        | 582          |
| US/Central        | 583          |
| US/East-Indiana   | 584          |
| US/Eastern        | 585          |
| US/Hawaii         | 586          |
| US/Indiana-Starke | 587          |
| US/Michigan       | 588          |
| US/Mountain       | 589          |
| US/Pacific        | 590          |
| US/Samoa          | 591          |
+-------------------+--------------+

Then use the "Name" value for the second argument:

select convert_tz(insert_timestamp,'UTC','US/Central') from table;

This approach works better than hard-coding a numeric offset from UTC, since that number would change depending on whether or not daylight savings is in effect.

Millham answered 3/1, 2024 at 17:36 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.