MySQL查询当前GMT时间

发布于 2024-08-20 23:18:50 字数 585 浏览 5 评论 0原文

这听起来很简单,但我一直无法弄清楚如何使用简单的 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 中的当前时间。预先感谢您的帮助!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

怪我闹别瞎闹 2024-08-27 23:18:50

只需使用 UTC(不受夏令时影响)

SELECT UTC_TIMESTAMP();

旧内容供参考:

这应该有效,但

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;

ok 并不完全是你想要的(它有 4 个查询,但只有一个选择:-)

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

宫墨修音 2024-08-27 23:18:50

CONVERT_TZ() 中没有错误

要使用 CONVERT_TZ(),您需要安装时区表,否则 MySql 将返回 NULL。

从 CLI 以 root 身份运行以下命令

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

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

谢谢

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

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)

丶情人眼里出诗心の 2024-08-27 23:18:50

笔记:
GMT 可能有 DST
UTC 没有 DST

SELECT UTC_TIMESTAMP();

我在这里做了一个备忘单: MySQL 应该有它的时区设置为 UTC?

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?

愛上了 2024-08-27 23:18:50

获取当前 UTC 日期时间的可靠方法是:

SELECT CONVERT_TZ(NOW(), @@session.time_zone, '+0:00')

The surefire way to fetch the current UTC datetime is:

SELECT CONVERT_TZ(NOW(), @@session.time_zone, '+0:00')
飞烟轻若梦 2024-08-27 23:18:50

在看到上面的所有答案并发现转换为 PST 不可靠后,我只是使用了这个:

DATE_SUB(用户上次登录,间隔 7 小时)

After seeing all the answers above and seeing it's unreliable to convert to PST, I just used this:

DATE_SUB(user_last_login, INTERVAL 7 hour)

一场春暖 2024-08-27 23:18:50

这应该有效 - 我认为您指定的时区是错误的。以芝加哥为例

SELECT CONVERT_TZ(NOW(), 'America/Chicago', 'GMT')

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

SELECT CONVERT_TZ(NOW(), 'America/Chicago', 'GMT')
金兰素衣 2024-08-27 23:18:50

当源服务器时间戳未知但要求特定的 tz 时,请执行以下操作:

select convert_tz(utc_timestamp(),'+05:00','+00:00')

这里以 CST 为例。
无论您在何处查询,utc_timestamp 都会保证“+00:00”结果。
“+05:00”是您想要的偏移量。
当访问多个未知服务器时,这非常有效,并且它可以确保您返回的结果都将位于公共 tz 结果集中。

When the source server timestamp is unknown but the requirement is for a specific tz do this:

select convert_tz(utc_timestamp(),'+05:00','+00:00')

This uses CST as an example.
utc_timestamp will guarantee a '+00:00' result no matter where you query it.
The '+05:00' is your desired offset.
This works great when hitting multiple unknown servers and it assures you that you returning result will all be in a common tz result set.

殊姿 2024-08-27 23:18:50

我正在搜索我的时区,即 GMT+6(亚洲/达卡时区)

MySQL 服务器位于美国。下面的内容对我有用。

选择 CONVERT_TZ( UTC_TIMESTAMP( ) , '+06:00', '+00:00' )

I was searching for my time zone which is GMT+6 (Asia/Dhaka TimeZone)

MySQL server is in US. The below worked for me.

SELECT CONVERT_TZ( UTC_TIMESTAMP( ) , '+06:00', '+00:00' )

哎呦我呸! 2024-08-27 23:18:50

CONVERT_TZ 函数没有错误。你得到 NULL 因为你使用时区名称/缩写。除非你安装了 mysql.time_zone_name 表,否则 MySQL 不知道“GMT”、“PST”等意味着什么。但是,如果您使用数字,它总是有效。

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

返回 NULL

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

返回 2016-06-24 17:58:33

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文