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: 2021-04-06 19:54 UTC
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: derick (profile)
Status: Closed Package: Date/time related
PHP Version: 5.6Git-2016-11-04 (Git) OS: FreeBSD
Private report: No CVE-ID: None
 [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
 [2021-04-06 19:54 UTC] derick@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: derick
 [2021-04-06 19:54 UTC] derick@php.net
The fix for this bug has been committed.
If you are still experiencing this bug, try to check out latest source from https://github.com/php/php-src and re-test.
Thank you for the report, and for helping us make PHP better.

Fixed for PHP 8.1.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 18 22:01:28 2024 UTC