|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #72470 modify() doesn't take into account DST changes
Submitted: 2016-06-22 19:58 UTC Modified: 2017-03-19 12:02 UTC
From: matthieux dot dekamen at gmail dot com Assigned:
Status: Not a bug Package: Date/time related
PHP Version: 7.1.0alpha1 OS:
Private report: No CVE-ID: None
 [2016-06-22 19:58 UTC] matthieux dot dekamen at gmail dot com
modify() doesn't take into account DST changes.

For example, if I add 23 hours with DateInterval where a DST change occurs:

echo date_default_timezone_get(); // prints Europe/Amsterdam
$dateTime = new DateTime('2016-10-29 10:00:00');
$dateTime->add(new DateInterval('PT23H'));

I get the expected result: 2016-10-30T08:00:00+01:00 (clock went backwards for 1 hour)

But if I add 23 hours with the modify() function on the same date:

$dateTime = new DateTime('2016-10-29 10:00:00');
$dateTime->modify('+ 23 hours');

I get: 2016-10-30T09:00:00+01:00
As you see the result here is different. modify() seems to not take into account DST changes.


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2016-06-22 20:10 UTC]
-Type: Bug +Type: Documentation Problem
 [2016-06-22 20:10 UTC]
add() and modify() have different meanings. After all, two methods that did the same thing would be a bit odd.

modify() will literally alter the date/time according to what you tell it. If you say "+23 hours" then it will add 23 to the "hour" value and adjust for overflow. 1000 + 23 hours = 3300 -> 0900 the next day.

add() does what you are expecting: add an interval of time. It's generally the same thing, except during a DST transition the interval will be appear shorter or longer on the clock. 23 hours after 1000 = 0900 the next day - 1 hour "lost" due to the DST transition = 0800 the next day.

Which gives me an idea for a bug report.
 [2017-03-19 12:02 UTC]
-Status: Open +Status: Not a bug
 [2017-03-19 12:02 UTC]
As explained in the comments this is not a bug
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Jun 15 18:01:30 2024 UTC