|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2016-10-06 02:37 UTC] mcasey at pegasustech dot com dot au
Description:
------------
It seems that strtotime is incorrectly calculating time adjustments across a DST change point.
I have tried this on 5.2.14 and 7.0.6 - same failure in both cases
The results of the code below, the start time should be 00:00:00 of the day before.
In Sydney Australia, time went forward at 2 am on the 2nd of October.
Using the -1 days feature of strtotime from the 3rd October at 00:00:00 returns
1st October 23:00:00 but should be 2 October 00:00:00
See results of program below.
Line 2 of the result shows Start: 2016-10-01 23:00:00 when it should be Start: 2016-10-02 00:00:00
Test script:
---------------
<?php
date_default_timezone_set ('Australia/Sydney');
#date_default_timezone_set ('America/Los_Angeles');
$time_now= strtotime( "02-10-2016 00:30:00");
$end_time = strtotime(date('c',$time_now)." midnight");
$start_time = strtotime(date('c',$time_now)." midnight -1 days");
echo "\nNow: ".date('Y-m-d H:i:s',$time_now)."< Start: ".date('Y-m-d H:i:s' ,$start_time)."< End: ".date('Y-m-d H:i:s' ,$end_time)."<\n\n";
$time_now= strtotime( "03-10-2016 00:30:00");
$end_time = strtotime(date('c',$time_now)." midnight");
$start_time = strtotime(date('c',$time_now)." midnight -1 days");
echo "Now: ".date('Y-m-d H:i:s',$time_now)."< Start: ".date('Y-m-d H:i:s' ,$start_time)."< End: ".date('Y-m-d H:i:s' ,$end_time)."<\n\n";
$time_now= strtotime( "04-10-2016 00:30:00");
$end_time = strtotime(date('c',$time_now)." midnight");
$start_time = strtotime(date('c',$time_now)." midnight -1 days");
echo "Now: ".date('Y-m-d H:i:s',$time_now)."< Start: ".date('Y-m-d H:i:s' ,$start_time)."< End: ".date('Y-m-d H:i:s' ,$end_time)."<\n\n";
?>
Expected result:
----------------
Now: 2016-10-02 00:30:00< Start: 2016-10-01 00:00:00< End: 2016-10-02 00:00:00<
Now: 2016-10-03 00:30:00< Start: 2016-10-02 00:00:00< End: 2016-10-03 00:00:00<
Now: 2016-10-04 00:30:00< Start: 2016-10-03 00:00:00< End: 2016-10-04 00:00:00<
Actual result:
--------------
Now: 2016-10-02 00:30:00< Start: 2016-10-01 00:00:00< End: 2016-10-02 00:00:00<
Now: 2016-10-03 00:30:00< Start: 2016-10-01 23:00:00< End: 2016-10-03 00:00:00<
Now: 2016-10-04 00:30:00< Start: 2016-10-03 00:00:00< End: 2016-10-04 00:00:00<
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Nov 02 23:00:02 2025 UTC |
Looks like the "midnight -1 day" doesn't subtract 1 day but 24 hours. As DST was introduced on the 2nd of Okt. 2016 in Sydney the 2nd of Okt. only had 23 hours so subtracting 24 hours will result in the 1st of Oct 23:00:00. DateTime-objects seem at first glance to be correct in that: $time_now= strtotime( "02-10-2016 00:30:00"); $end_time = new DateTime(date('c',$time_now)." midnight - 1 day"); var_Dump($end_time->format('c')); // string(25) "2016-10-02T00:00:00+11:00" But looking in more detail it's also a mess here, as the 2nd of October 00:00:00 should be with a time-offset of 10 hours and not 11 hours…I'm having the same issue. Mine occurs when I add 1 day to a date. It seems to add 24 hours instead, causing problems to me when I deal with dates on and off summer time. Example code to reproduce the error: $dates = null; $start = strtotime('2016-08-10 00:00:00'); $date = $start; while ($data <= strtotime('2016-10-17')) { //It works if I change it to '2016-10-17 01:00:00' $dates[] = date('Y-m-d', $date); $date = strtotime('+1 day', $date); }