php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #73255 strtotime -1 day across DST change adds extra hour
Submitted: 2016-10-06 02:37 UTC Modified: 2016-10-06 09:32 UTC
From: mcasey at pegasustech dot com dot au Assigned:
Status: Open Package: Date/time related
PHP Version: 7.0Git-2016-10-06 (Git) OS: Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
50 - 6 = ?
Subscribe to this entry?

 
 [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<

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-10-06 09:32 UTC] cmb@php.net
-Package: date_time +Package: Date/time related
 [2017-01-16 17:19 UTC] heiglandreas@php.net
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…
 [2017-03-09 17:43 UTC] guilherme dot passero at unifebe dot edu dot br
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);
}
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Fri Oct 18 10:01:27 2019 UTC