开发者

How to get #entries of last hour in MySQL, correcting for timezone?

开发者 https://www.devze.com 2022-12-27 08:02 出处:网络
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:

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(CU开发者_如何转开发RRENT_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?


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:

SELECT COUNT(id) as cnt FROM karmalog WHERE user_id = ' 89' AND event='IMG_UPD' 
AND date_created > DATE_SUB(UTC_TIMESTAMP(), INTERVAL 1 HOUR);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号