Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL query for current GMT time

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!

like image 691
Russell C. Avatar asked Feb 13 '10 16:02

Russell C.


People also ask

How do I get current time zone in MySQL?

The following is the syntax to get the current time zone of MySQL. mysql> SELECT @@global. time_zone, @@session.

Is MySQL now UTC?

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.

How do I find the timezone of a database?

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.

What is current time in MySQL?

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.


Video Answer


4 Answers

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 :-)

like image 122
Rufinus Avatar answered Oct 02 '22 00:10

Rufinus


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)

like image 40
Stephen Pritchard Avatar answered Oct 02 '22 00:10

Stephen Pritchard


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?

like image 35
Timo Huovinen Avatar answered Sep 29 '22 00:09

Timo Huovinen


The surefire way to fetch the current UTC datetime is:

SELECT CONVERT_TZ(NOW(), @@session.time_zone, '+0:00')
like image 40
ugh StackExchange Avatar answered Oct 02 '22 00:10

ugh StackExchange