php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #81072 Incorrect behavior of DateTime->modify across DST changes
Submitted: 2021-05-22 18:55 UTC Modified: 2022-08-12 14:27 UTC
Votes:3
Avg. Score:5.0 ± 0.0
Reproduced:3 of 3 (100.0%)
Same Version:1 (33.3%)
Same OS:2 (66.7%)
From: ph-bugs at allenjb dot me dot uk Assigned:
Status: Not a bug Package: Date/time related
PHP Version: 8.0.6 OS:
Private report: No CVE-ID: None
 [2021-05-22 18:55 UTC] ph-bugs at allenjb dot me dot uk
Description:
------------
See https://3v4l.org/hGXcr

Working on a fix for https://github.com/dragonmantank/cron-expression/issues/111

As you can see, when working backwards with ->modify("-1 hour") DateTime fails to properly handle the DST change and keeps pushing the time forward to 03:00:00

Working forwards with ->modify("+1 hour") does not exhibit the same behavior.

(The code examples also show a workaround, which involved switching timezones while performing the ->modify)

Test script:
---------------
<?php

function incrementWithSwitch($date, $invert)
{
    $timezone = $date->getTimezone();
    $date = $date->setTimezone(new DateTimeZone('UTC'));
    $date = $date->modify(($invert ? '-' : '+') . '1 hour');
    $date = $date->setTimezone($timezone);

    $date = $date->setTime((int)$date->format('H'), 0);
    return $date;
}


function incrementWithoutSwitch($date, $invert)
{
    $date = $date->modify(($invert ? '-' : '+') . '1 hour');
    return $date;
}

$tz = new \DateTimeZone("Europe/London");
$dtWith = \DateTimeImmutable::createFromFormat("!Y-m-d H:i:s", "2021-03-28 04:00:00");
$dtWithout = $dtWith;

$invert = true;
for ($i = 0; $i < 3; $i++) {
    print "\nRound {$i}:";
    $dtWith = incrementWithSwitch($dtWith, $invert);
    $dtWithout = incrementWithoutSwitch($dtWithout, $invert);
    print "With switch:    ". $dtWith->format(\DateTime::RFC3339);
    print " :: Without switch: ". $dtWithout->format(\DateTime::RFC3339);
}

print "\n---\n";

$dtWith = \DateTimeImmutable::createFromFormat("!Y-m-d H:i:s", "2021-03-28 00:00:00");
$dtWithout = $dtWith;

$invert = false;
for ($i = 0; $i < 3; $i++) {
    print "\nRound {$i}:";
    $dtWith = incrementWithSwitch($dtWith, $invert);
    $dtWithout = incrementWithoutSwitch($dtWithout, $invert);
    print "With switch:    ". $dtWith->format(\DateTime::RFC3339);
    print " :: Without switch: ". $dtWithout->format(\DateTime::RFC3339);
}

Expected result:
----------------
Round 0:With switch:    2021-03-28T03:00:00+02:00 :: Without switch: 2021-03-28T03:00:00+02:00
Round 1:With switch:    2021-03-28T01:00:00+01:00 :: Without switch: 2021-03-28T01:00:00+02:00
Round 2:With switch:    2021-03-28T00:00:00+01:00 :: Without switch: 2021-03-28T00:00:00+02:00
---

Round 0:With switch:    2021-03-28T01:00:00+01:00 :: Without switch: 2021-03-28T01:00:00+01:00
Round 1:With switch:    2021-03-28T03:00:00+02:00 :: Without switch: 2021-03-28T03:00:00+02:00
Round 2:With switch:    2021-03-28T04:00:00+02:00 :: Without switch: 2021-03-28T04:00:00+02:00

Actual result:
--------------
Round 0:With switch:    2021-03-28T03:00:00+02:00 :: Without switch: 2021-03-28T03:00:00+02:00
Round 1:With switch:    2021-03-28T01:00:00+01:00 :: Without switch: 2021-03-28T03:00:00+02:00
Round 2:With switch:    2021-03-28T00:00:00+01:00 :: Without switch: 2021-03-28T03:00:00+02:00
---

Round 0:With switch:    2021-03-28T01:00:00+01:00 :: Without switch: 2021-03-28T01:00:00+01:00
Round 1:With switch:    2021-03-28T03:00:00+02:00 :: Without switch: 2021-03-28T03:00:00+02:00
Round 2:With switch:    2021-03-28T04:00:00+02:00 :: Without switch: 2021-03-28T04:00:00+02:00

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-05-22 20:17 UTC] requinix@php.net
-Status: Open +Status: Feedback
 [2021-05-22 20:17 UTC] requinix@php.net
Looks like 3v4l.org is behind a few versions - maybe was impacted by the GitHub move.

I can't reproduce locally. Can you test in current master (8.1)? If it works there then this may be a duplicate of 74274.
 [2021-05-23 09:45 UTC] php-bugs at allenjb dot me dot uk
I've tested this with current master (8.1-dev) and the issue still exists.

I noticed a bug in the reproduction script I previously posted and have fixed it. Updated script: https://3v4l.org/01oD9

<?php

function incrementWithSwitch($date, $invert)
{
    $timezone = $date->getTimezone();
    $date = $date->setTimezone(new DateTimeZone('UTC'));
    $date = $date->modify(($invert ? '-' : '+') . '1 hour');
    $date = $date->setTimezone($timezone);

    $date = $date->setTime((int)$date->format('H'), 0);
    return $date;
}


function incrementWithoutSwitch($date, $invert)
{
    $date = $date->modify(($invert ? '-' : '+') . '1 hour');
    return $date;
}

$tz = new \DateTimeZone("Europe/London");
$dtWith = \DateTimeImmutable::createFromFormat("!Y-m-d H:i:s", "2021-03-28 04:00:00", $tz);
$dtWithout = $dtWith;

$invert = true;
for ($i = 0; $i < 4; $i++) {
    print "\nRound {$i}:";
    $dtWith = incrementWithSwitch($dtWith, $invert);
    $dtWithout = incrementWithoutSwitch($dtWithout, $invert);
    print "With switch:    ". $dtWith->format(\DateTime::RFC3339);
    print " :: Without switch: ". $dtWithout->format(\DateTime::RFC3339);
}

print "\n---\n";

$dtWith = \DateTimeImmutable::createFromFormat("!Y-m-d H:i:s", "2021-03-28 00:00:00");
$dtWithout = $dtWith;

$invert = false;
for ($i = 0; $i < 4; $i++) {
    print "\nRound {$i}:";
    $dtWith = incrementWithSwitch($dtWith, $invert);
    $dtWithout = incrementWithoutSwitch($dtWithout, $invert);
    print "With switch:    ". $dtWith->format(\DateTime::RFC3339);
    print " :: Without switch: ". $dtWithout->format(\DateTime::RFC3339);
}
 [2021-05-30 04:22 UTC] php-bugs at lists dot php dot net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Re-Opened". Thank you.
 [2021-05-30 06:33 UTC] requinix@php.net
-Status: No Feedback +Status: Open
 [2021-07-30 10:43 UTC] antonino dot spampinato86 at gmail dot com
This is the refined version proposed. DST transition bug for modify function exists. Bug 74224 mentions that from php 8.1 that diff function has been changed and supports time not conversion to UTC and this makes time or date calculations consistent with local time. Actually diff function also suffers from DST or ST problems, If you deem it necessary I will also show it to you.

function incrementWithSwitch($date, $invert)
{
    $timezone = $date->getTimezone();
    $date = $date->setTimezone(new \DateTimeZone('UTC'));
    $date = $date->modify(($invert ? '-' : '+') . '1 hour');
    $date = $date->setTimezone($timezone);

    //$date = $date->setTime((int)$date->format('H'), 0);
    return $date;
}


function incrementWithoutSwitch($date, $invert)
{
    $date = $date->modify(($invert ? '-' : '+') . '1 hour');
    return $date;
}

$tz = new \DateTimeZone('Europe/London');
$dtWith = \DateTimeImmutable::createFromFormat('!Y-m-d H:i:s', '2021-03-28 04:00:00', $tz);
$dtWithout = $dtWith;

$invert = true;
$string = 'Start ' . $dtWith->format('Y-m-d H:i:s Z') . PHP_EOL;
for ($i = 0; $i < 4; $i++) {
    $string .= "Round {$i}: ";
    $dtWith = incrementWithSwitch($dtWith, $invert);
    $dtWithout_test1 = incrementWithoutSwitch($dtWithout, $invert);
    $string .= 'With switch:    ' . $dtWith->format('Y-m-d H:i:s Z');
    $string .= ' :: Without switch: '. $dtWithout->format('Y-m-d H:i:s Z') . PHP_EOL;
}

$string .= '---' . PHP_EOL;
$dtWith = \DateTimeImmutable::createFromFormat('!Y-m-d H:i:s', '2021-03-27 23:00:00', $tz);
$dtWithout = $dtWith;

$invert = false;
$string .= 'Start ' . $dtWith->format('Y-m-d H:i:s Z') . PHP_EOL;
for ($i = 0; $i < 4; $i++) {
    $string .= "Round {$i}: ";
    $dtWith = incrementWithSwitch($dtWith, $invert);
    $dtWithout = incrementWithoutSwitch($dtWithout, $invert);
    $string .= 'With switch:    ' . $dtWith->format('Y-m-d H:i:s Z');
    $string .= ' :: Without switch: ' . $dtWithout->format('Y-m-d H:i:s Z') . PHP_EOL;
}
print($string);

Expected:
Start 2021-03-28 04:00:00 3600
Round 0: With switch:    2021-03-28 03:00:00 3600 :: Without switch: 2021-03-28 03:00:00 3600
Round 1: With switch:    2021-03-28 02:00:00 3600 :: Without switch: 2021-03-28 02:00:00 3600
Round 2: With switch:    2021-03-28 00:00:00 0 :: Without switch: 2021-03-28 00:00:00 3600
Round 3: With switch:    2021-03-27 23:00:00 0 :: Without switch: 2021-03-27 23:00:00 3600
---
Start 2021-03-27 23:00:00 0
Round 0: With switch:    2021-03-28 00:00:00 0 :: Without switch: 2021-03-28 00:00:00 0
Round 1: With switch:    2021-03-28 02:00:00 3600 :: Without switch: 2021-03-28 02:00:00 3600
Round 2: With switch:    2021-03-28 03:00:00 3600 :: Without switch: 2021-03-28 03:00:00 3600
Round 3: With switch:    2021-03-28 04:00:00 3600 :: Without switch: 2021-03-28 04:00:00 3600

Actual: Start 2021-03-28 04:00:00 3600
Round 0: With switch:    2021-03-28 03:00:00 3600 :: Without switch: 2021-03-28 04:00:00 3600
Round 1: With switch:    2021-03-28 02:00:00 3600 :: Without switch: 2021-03-28 04:00:00 3600
Round 2: With switch:    2021-03-28 00:00:00 0 :: Without switch: 2021-03-28 04:00:00 3600
Round 3: With switch:    2021-03-27 23:00:00 0 :: Without switch: 2021-03-28 04:00:00 3600
---
Start 2021-03-27 23:00:00 0
Round 0: With switch:    2021-03-28 00:00:00 0 :: Without switch: 2021-03-28 00:00:00 0
Round 1: With switch:    2021-03-28 02:00:00 3600 :: Without switch: 2021-03-28 02:00:00 3600
Round 2: With switch:    2021-03-28 03:00:00 3600 :: Without switch: 2021-03-28 03:00:00 3600
Round 3: With switch:    2021-03-28 04:00:00 3600 :: Without switch: 2021-03-28 04:00:00 3600
 [2021-07-30 10:53 UTC] antonino dot spampinato86 at gmail dot com
Sorry change $dtWithout_test1 = incrementWithoutSwitch($dtWithout, $invert); to $dtWithout = incrementWithoutSwitch($dtWithout, $invert);
The bug is there, the expected result is not obtained. :-)
 [2021-10-01 18:25 UTC] cmb@php.net
-Status: Open +Status: Verified
 [2021-10-01 18:25 UTC] cmb@php.net
> As you can see, when working backwards with ->modify("-1 hour")
> DateTime fails to properly handle the DST change and keeps pushing
> the time forward to 03:00:00

So this about <https://3v4l.org/XWdf2>, right?

If so, yes, that doesn't look right.
 [2022-04-29 09:47 UTC] r dot aalderink at visymo dot com
Ran into the same issue, I have a very simple oneliner to reproduce it:

-----
echo (new \DateTime('2022-03-13 03:00:00', new \DateTimeZone('America/Los_Angeles')))->modify('-5 minutes')->format('Y-m-d H:i:s');
-----

This becomes: 2022-03-13 03:55:00
Expected: 2022-03-13 01:55:00
 [2022-08-12 14:27 UTC] derick@php.net
-Status: Verified +Status: Not a bug
 [2022-08-12 14:27 UTC] derick@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

The result is, although perhaps surprising, expected.

Just like any other overflow that happens, PHP moves the wall time forwards to what is possible.

It is the same effect as doing:

echo (new \DateTime("2022-03-31"))->modify("-1 month")->format('Y-m-d H:i:s O T'), "\n";

which outputs:

2022-03-03 00:00:00 +0000 UTC

There is no February 31st. The 31st day since February 0th, is March 3rd.

The same happens in this case:

new \DateTimeImmutable('2022-03-13 03:00:00', new \DateTimeZone('America/Los_Angeles'))->modify('-5 minutes');

This resolves first to 2022-03-13 02:55:00 (PST), which does not exist. 2h and 55 minutes after the start of the day (2022-03-13 00:00:00 PST) is 2022-03-13 03:55:00 PDT.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Apr 20 00:01:27 2024 UTC