如何在 MySQL 中获取最后一小时的#entries,并纠正时区?
我将活动条目存储在 MySQL 表中。该表有一个时间戳类型的 date_created 字段,通过 PHP,我根据 GMT 插入活动条目:
$timestamp = gmdate("Y-m-d H:i:s", time());
这工作正常。在我的客户端上,我的时间是 GMT+2。如果这里是 16:00,我插入一个条目,它在 MySQL 中存储为 14:00。这正如我所预料的那样。我的
现在我想获取过去一小时内来自 MySQL 的活动条目数。我正在使用以下查询:
SELECT COUNT(id) as cnt FROM karmalog WHERE user_id = ' 89' AND event='IMG_UPD'
AND date_created > DATE_SUB(CURRENT_TIMESTAMP, INTERVAL 1 HOUR)
这不会返回任何内容,因为 CURRENT_TIMESTAMP 使用 MySQL 时区设置,该时区设置为 SYSTEM,该时区设置为 GMT+2。我想我正在寻找的是MySQL中的GMT_CURRENTTIMESTAMP,有这样的东西吗?
I am storing activity entries in a MySQL table. The table has a date_created field of type timestamp and through PHP I insert the activity entries based on GMT:
$timestamp = gmdate("Y-m-d H:i:s", time());
This works fine. On my client I am on GMT+2. If it is 16:00 here and I insert an entry, it is stored in MySQL as 14:00. This is as expected I guess. My
Now I would like to get the number of activity entries from MySQL within the last hour. I'm using the following query:
SELECT COUNT(id) as cnt FROM karmalog WHERE user_id = ' 89' AND event='IMG_UPD'
AND date_created > DATE_SUB(CURRENT_TIMESTAMP, INTERVAL 1 HOUR)
This returns nothing, because CURRENT_TIMESTAMP uses the MySQL timezone settings, which is set to SYSTEM, which is set to GMT+2. I guess what I am looking for is a GMT_CURRENTTIMESTAMP in MySQL, is there such a thing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有 UTC_TIMESTAMP() 函数,它以上下文相关的方式返回当前 UTC 时间戳。您可以使用它来代替
CURRENT_TIMESTAMP
:There is the UTC_TIMESTAMP() function, which returns the current UTC timestamp in a contextually-sensitive manner. You can use that in place of
CURRENT_TIMESTAMP
: