MySQL query for current GMT time

后端 未结 9 1256
你的背包
你的背包 2020-12-24 07:39

This sounds simple enough but I haven\'t been able to figure out how to use a simple SELECT statement to return the current time in GMT.

I have been trying to use CO

相关标签:
9条回答
  • 2020-12-24 08:08

    The surefire way to fetch the current UTC datetime is:

    SELECT CONVERT_TZ(NOW(), @@session.time_zone, '+0:00')
    
    0 讨论(0)
  • 2020-12-24 08:08

    There is no bug in CONVERT_TZ function. You get NULL because you use time zones names/abbreviations. Mysql does not know what this 'GMT','PST', etc means unless you install mysql.time_zone_name table. However if you use numbers it will always work.

    mysql> SELECT CONVERT_TZ(NOW(), 'America/Chicago', 'GMT');
    

    returns NULL

    mysql> SELECT CONVERT_TZ(NOW(),'-08:00','+00:00');
    

    returns 2016-06-24 17:58:33

    0 讨论(0)
  • 2020-12-24 08:11

    NO BUG in CONVERT_TZ()

    To use CONVERT_TZ() you need to install the time-zone tables otherwise MySql returns NULL.

    From the CLI run the following as root

    # mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root mysql

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

    Thanks

    http://www.ArcanaVision.com (SJP 2011-08-18)

    0 讨论(0)
  • 2020-12-24 08:17

    Just use UTC (doesnt get affected with daylight savings time)

    SELECT UTC_TIMESTAMP();
    

    Old Content for reference:

    this should work, but with

    SELECT CONVERT_TZ(NOW(),'PST','GMT');
    

    i got also NULL as result. funny enough the example in the mysql docu also returns null

    SELECT CONVERT_TZ('2004-01-01 12:00:00','GMT','MET');
    

    http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_convert-tz seems you found a bug in mysql. (thanks to +Stephen Pritchard)

    you could try:

    SET @OLD_TIME_ZONE=@@TIME_ZONE;
    SET TIME_ZONE='+00:00';
    SELECT NOW();
    SET TIME_ZONE=@OLD_TIME_ZONE;
    

    ok is not exactly what you wanted (its 4 queries, but only one select :-)

    0 讨论(0)
  • 2020-12-24 08:17

    Note: GMT might have DST UTC does not have DST

    SELECT UTC_TIMESTAMP();
    

    I made a cheatsheet here: Should MySQL have its timezone set to UTC?

    0 讨论(0)
  • 2020-12-24 08:17

    This should work - I think you are specified your timezone wrong. Using Chicago as example

    SELECT CONVERT_TZ(NOW(), 'America/Chicago', 'GMT')
    
    0 讨论(0)
提交回复
热议问题