开发者

Will the timezone used in MySQL Date and Time functions cause conflicts/issues?

开发者 https://www.devze.com 2023-04-08 20:41 出处:网络
I have a website which allows visitors to view content, and has the option for them to register and login (which gives them permission to submit content).

I have a website which allows visitors to view content, and has the option for them to register and login (which gives them permission to submit content).

  • Whenever content is submitted by users I do a INSERT INTO query in the content table, I use UNIX_TIMESTAMP() to store the timestamp in the column content_timestamp (which is the time of when the content is submitted).

  • I also allow users to optionally select a timezone (supports all timezones from http://php.net/manual/en/timezones.php) from a dropdown menu in there usercp - and a column (user_timezone) in the users table gets UPDATE'd with that, so all displayed dates are adjusted to that (if selected).

  • One the page which the content is displayed I display the formatted date (of when the content is submitted) - if they are not logged or have logged in and not selected a timezone (in the usercp) I format it in GMT otherwise I use there selected timezone e.g.:


<?php
if (!is_logged_in() || is_logged_in() && empty($row['user_timezone'])) {
   echo gmdate('d/m/y', $row['content_timestamp']) . ' GMT'; //in GMT
} else {
   date_default_timezone_set($row['user_timezone']);
   echo date('d/m/y', $row['content_timestamp']); //use the users timezone
} 
?>

Now everything is fine but now I'm planning on schedulling a cronjob weekly and monthly (this will be the weekly featured and monthly featured content) which will access a php file which will do a INSERT INTO query (of top rated content) and add it to the featured table - along with the UNIX_TIMESTAMP() (featured_timestamp - of when it was added).

The cronjob is set to access the php file the first day of every week 12 am 开发者_开发问答midnight GMT and the first day of every month 12 am midnight GMT.

...Now getting to the problem:

I'm displaying the featured content by comparing timestamps using the MySQL Date and Time functions like so (below example will return the weekly featured content):

SELECT * FROM featured WHERE WEEK(FROM_UNIXTIME(featured_timestamp), 1) = WEEK(UNIX_TIMESTAMP(), 1) AND
FROM_UNIXTIME(featured_timestamp, '%Y %M') = FROM_UNIXTIME(UNIX_TIMESTAMP(), '%Y %M')

So will this cause any timezone conflicts/issues seeing as the cronjob is accessing the file in GMT and I'm unaware of what timezone MySQL WEEK() and DATE_FORMAT() use (just curious as that would mean comparison queries like the above could produce incorrect results)?

0

精彩评论

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

关注公众号