MySQL查询当前GMT时间 [英] MySQL query for current GMT time
问题描述
这听起来很简单,但是我还没有想出如何使用简单的SELECT语句来返回GMT的当前时间。
我已经尝试使用 CONVERT_TZ()根据服务器时区和GMT时区将NOW()转换为GMT,但是由于某种原因,当我放入文本时区时,它返回NULL。我得到结果的唯一方法是实际放置偏移量,这种偏移方式太复杂,应该是一个非常简单的操作。这是我的意思:
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
我需要的是一个简单的查询以GMT格式返回当前时间。感谢您的帮助!
这应该可以工作,但与
SELECT CONVERT_TZ(NOW(),'PST','GMT');
我的结果也是NULL。有趣的是mysql中的示例也返回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
似乎你在mysql中发现了一个错误。 (感谢+ Stephen Pritchard)
您可以尝试:
SET @OLD_TIME_ZONE = @@ TIME_ZONE;
SET TIME_ZONE ='+ 00:00';
SELECT NOW();
SET TIME_ZONE = @ OLD_TIME_ZONE;确定不是你想要的(它的4个查询,但只有一个选择: - )
或您使用UTC(不受夏令时的影响)
SELECT UTC_TIMESTAMP();
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!
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 :-)
or you use UTC (doesnt get affected with daylight savings time)
SELECT UTC_TIMESTAMP();
这篇关于MySQL查询当前GMT时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!