PHP strtotime() function wrong by 1 hour?

我的未来我决定 提交于 2019-12-18 14:48:14

问题


I am converting dates and times into timestamps using PHP before they are inserted into my MySQL database.

My problem is that when i use PHP's strtotime function the output timestamp is -1 hour behind my actual time.

For example, considering todays date: 07/21/2010. When i use php code like:

<?php
$my_timestamp = strtotime("07/21/2010");
echo $my_timestamp;
?>

The timestamp sent back is the GMT equivilent MINUS 1 hour. i.e. i get back: 20 Jul 2010 23:00:00 GMT instead of 21 Jul 2010 00:00:00 GMT.

I live in the UK so my timezone is GMT. I have declared my timezone in the script using date_default_timezone_set('Europe/London') and i have also ensured that the php.ini file is set to 'Europe/London'.

Is this something to do with daylight savings time perhaps? How can i fix the problem without adding 1 hour to all my dates?


回答1:


Europe/London time is not GMT time during Daylight Savings. You need to set it to UTC.

date_default_timezone_set('UTC');

date_default_timezone_set('GMT'); may work, but, as Kenneth notes in a comment below, it is deprecated.




回答2:


London time zone is expressed in British Summer Time during Summer. Having said that, it's a good practice to store time in UTC and present the time to end user in either UTC or in THEIR local time.

It is also probably wise to ensure that your system time is UTC.




回答3:


Before PHP 5.3 the strtotime had problems calculating time if you added or deducted months and days etc. That was fixed so you now can tell specifically how you want it calculated, if you are below PHP 5.3 I would recommend doing the date calculating in mysql or upgrade your PHP version.



来源:https://stackoverflow.com/questions/3303970/php-strtotime-function-wrong-by-1-hour

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!