php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #73460 Datetime add not realising it already applied DST change
Submitted: 2016-11-04 15:25 UTC Modified: -
Votes:3
Avg. Score:5.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:1 (50.0%)
From: saulyx at gmail dot com Assigned:
Status: Open Package: Date/time related
PHP Version: 5.6Git-2016-11-04 (Git) OS: FreeBSD
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:
16 + 40 = ?
Subscribe to this entry?

 
 [2016-11-04 15:25 UTC] saulyx at gmail dot com
Description:
------------
PHP seems to run into an infinite loop when add() is run and it adjusts to the DST change, but when it arrives at the time again, it reset back an hour for infinite amount of times. 



Test script:
---------------
$date = new DateTime('2016-11-05 22:00:00', new DateTimeZone('America/New_York'));

foreach(range(1,48) as $i) {
	echo $date->format('Y/m/d H:i')."<br/>";
						
	$date->add(new DateInterval('PT15M'));
}

Actual result:
--------------
2016/11/05 22:00
2016/11/05 22:15
2016/11/05 22:30
2016/11/05 22:45
2016/11/05 23:00
2016/11/05 23:15
2016/11/05 23:30
2016/11/05 23:45
2016/11/06 00:00
2016/11/06 00:15
2016/11/06 00:30
2016/11/06 00:45
2016/11/06 01:00
2016/11/06 01:15
2016/11/06 01:30
2016/11/06 01:45
2016/11/06 01:00
2016/11/06 01:15
2016/11/06 01:30
2016/11/06 01:45
2016/11/06 01:00
2016/11/06 01:15
2016/11/06 01:30
2016/11/06 01:45
2016/11/06 01:00
2016/11/06 01:15
2016/11/06 01:30
2016/11/06 01:45
2016/11/06 01:00
2016/11/06 01:15
2016/11/06 01:30
2016/11/06 01:45
2016/11/06 01:00
2016/11/06 01:15
2016/11/06 01:30
2016/11/06 01:45
2016/11/06 01:00
2016/11/06 01:15
2016/11/06 01:30
2016/11/06 01:45
2016/11/06 01:00
2016/11/06 01:15
2016/11/06 01:30
2016/11/06 01:45
2016/11/06 01:00
2016/11/06 01:15
2016/11/06 01:30
2016/11/06 01:45


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-03-22 21:56 UTC] fyrye3 at gmail dot com
The same endless loop issue can also be seen with DateTime::sub() when DST starts.

Example demonstrating both issues: https://3v4l.org/ZNfgq
---
<?php

date_default_timezone_set('America/New_York');

//DST starts Apr. 2nd 02:00 and moves to 03:00
$start = new \DateTime('2006-04-02T01:00:00');
$end = new \DateTime('2006-04-02T04:00:00');

while($end > $start) {
    $now = clone $end;
    $end->sub(new \DateInterval('PT1H'));
    echo $end->format('Y-m-d H:i T') . PHP_EOL;
    if ($now == $end) {
        break; //remove this will result in an endless loop
    }
}

echo '-----' . \PHP_EOL;

//DST ends Oct. 29th 02:00 and moves to 01:00
$start = new \DateTime('2006-10-29T00:30:00');
$end = new \DateTime('2006-10-29T03:00:00');

$i = 0;
while($end > $start) {
    $now = clone $start;
    $start->add(new \DateInterval('PT30M'));
    echo $start->format('Y-m-d H:i T') . PHP_EOL;
    if ($i++ > 7) {
        break; //remove this will result in an endless loop
    }
}
?>

Received
-----------------------
2006-04-02 03:00 EDT
2006-04-02 03:00 EDT
-----
2006-10-29 00:30 EDT
2006-10-29 01:00 EDT
2006-10-29 01:30 EDT
2006-10-29 01:00 EST
2006-10-29 01:30 EDT
2006-10-29 01:00 EST
2006-10-29 01:30 EDT
2006-10-29 01:00 EST
2006-10-29 01:30 EDT


Expected
-----------------------
2006-04-02 03:00 EDT
2006-04-02 01:00 EST
---
2006-10-29 00:30 EDT
2006-10-29 01:00 EDT
2006-10-29 01:30 EDT
2006-10-29 01:00 EST
2006-10-29 01:30 EST
2006-10-29 02:00 EST
 [2019-03-22 22:10 UTC] fyrye3 at gmail dot com
Corrections to my previous comment

Example https://3v4l.org/lUaT9

Expected
-----------------------
2006-04-02 03:00 EDT
2006-04-02 01:00 EST
---
2006-10-29 01:00 EDT
2006-10-29 01:30 EDT
2006-10-29 01:00 EST
2006-10-29 01:30 EST
2006-10-29 02:00 EST
2006-10-29 02:30 EST
2006-10-29 03:00 EST
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Sun Nov 29 09:01:24 2020 UTC