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 CONVERT_TZ() to convert NOW() to GMT based on the server time zone and the GMT time zone but for some reason it returns NULL when I put in the text time zones. The only way I get a result is to actually put in the offsets which is getting way too complicated for what should be a really simple operation. Here is what I mean:
mysql> SELECT CONVERT_TZ(NOW(),@@global.system_time_zone,'GMT');
NULL
mysql> SELECT CONVERT_TZ(NOW(),'PST','GMT');
NULL
mysql> SELECT CONVERT_TZ(NOW(),'-08:00','+00:00');
2010-02-13 18:28:22
All I need is a simple query to return the current time in GMT. Thanks in advance for your help!
The following is the syntax to get the current time zone of MySQL. mysql> SELECT @@global. time_zone, @@session.
In MySQL, the UTC_TIMESTAMP returns the current UTC date and time as a value in 'YYYY-MM-DD HH:MM:SS' or YYYYMMDDHHMMSS. uuuuuu format depending on the usage of the function i.e. in a string or numeric context.
DBTIMEZONE returns the value of the database time zone. The return type is a time zone offset (a character type in the format '[+|-]TZH:TZM' ) or a time zone region name, depending on how the user specified the database time zone value in the most recent CREATE DATABASE or ALTER DATABASE statement.
MySQL CURRENT_TIME() Function The CURRENT_TIME() function returns the current time. Note: The time is returned as "HH-MM-SS" (string) or as HHMMSS. uuuuuu (numeric). Note: This function equals the CURTIME() function.
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 :-)
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)
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?
The surefire way to fetch the current UTC datetime is:
SELECT CONVERT_TZ(NOW(), @@session.time_zone, '+0:00')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With