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

后端 未结 3 997
夕颜
夕颜 2020-11-28 08:01

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

相关标签:
3条回答
  • 2020-11-28 08:23
     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

    0 讨论(0)
  • 2020-11-28 08:32

    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!

    0 讨论(0)
  • 2020-11-28 08:41

    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

    0 讨论(0)
提交回复
热议问题