简体   繁体   中英

PHP strtotime timezone issue with '0:00 GMT'

So I have a function get_start_of_day($time) , which I want to give me a timestamp representing the very very first minute of the day in which $time , a timestamp, falls.

Originally, I was using:

 function get_start_of_day($time) {
     return strtotime('0:00 GMT', $time);
 }

which seemed to be working just fine.

Then I realized that for certain times it was returning midnight of the previous day, for example:

$x = 1432468800;
echo gmdate('Y-M-d H:i:s', $x);
$y = get_start_of_day($x);
echo gmdate('Y-M-d H:i:s', $y);

outputs:

2015-May-24 12:00:00
2015-May-24 00:00:00

BUT:

$x = 1432432800;
echo gmdate('Y-M-d H:i:s', $x);
$y = get_start_of_day($x);
echo gmdate('Y-M-d H:i:s', $y);

outputs:

2015-May-24 02:00:00
2015-May-23 00:00:00

So at some value for $x, the output was wrongly jumping to the day previous and outputting that. Some further experimentation revealed that the value for $x at which the jump happens is 4am, ie the timestamp 1432440000 gave the proper result, but the timestamp 1432439999 did not.

Now, according to Epoch Converter I'm in GMT-4:00. It seems as though this must somehow be the cause. I've updated the function to:

 function get_start_of_day($time) {
     $start_of_day = strtotime('tomorrow 0:00 GMT', $time);
     if ($start_of_day > $time) {
         $start_of_day -= 86400;
     }
     return $start_of_day;
 }

which seems to work fine, but I can't help but wonder...what's going on here? Does anybody know?

Thanks in advance! :)

EDIT: I'm using GMT in case I move to a new server...I would hate to be getting different start-of-day values after I hypothetically move, which may not sync up with the old values. So I figured, just use GMT for everything, all the time.

Use strtotime('today', $time) . You will need to use date_default_timezone_set or date.timezone INI setting to select your time zone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM