|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #72243 DateTimes fail to compare correctly when a DateTimeZone is set
Submitted: 2016-05-19 18:13 UTC Modified: 2017-03-30 05:26 UTC
Avg. Score:4.5 ± 0.5
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:1 (50.0%)
From: Rican7 at gmail dot com Assigned: heiglandreas (profile)
Status: Closed Package: Date/time related
PHP Version: 7.0.6 OS:
Private report: No CVE-ID: None
 [2016-05-19 18:13 UTC] Rican7 at gmail dot com
DateTime instances are supposed to be natively "comparable" (using `==`, `>`, `<`, etc).

Unfortunately, a colleague and I just noticed that if a Time Zone is updated on a compared instance, than the comparison fails... but only if the time zone isn't an IANA identifier. Really strange. What's worse, is that it seems to "magically" start to work again if a (seemingly idempotent) `getTimestamp()` call is made on the instance that had the new time-zone applied. Is this due to a lazy calculation of the internal timestamp that isn't getting validated when a new time zone is applied?

Test script:

$datetime = new DateTime(null, new DateTimeZone('UTC'));
$datetime_2 = clone $datetime;
$datetime_2->add(new DateInterval('PT2S'));

var_dump($datetime < $datetime_2);
var_dump($datetime->getTimestamp() < $datetime_2->getTimestamp());

$datetime_2->setTimezone(new DateTimeZone('EST'));

var_dump($datetime < $datetime_2);
var_dump($datetime->getTimestamp() < $datetime_2->getTimestamp());

// Now that `getTimestamp()` has been called, it'll "magically" work...
var_dump($datetime < $datetime_2);

Expected result:

Actual result:


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2016-05-19 18:15 UTC] Rican7 at gmail dot com
These 3v4l scripts show the bug in action:

- Broken (TZ abbreviation):
- Working (IANA TZ):
 [2016-05-19 18:16 UTC] Rican7 at gmail dot com
May be related to bug #68474
 [2017-03-29 19:50 UTC] php-bugs at allenjb dot me dot uk
Refreshing the broken 3v4l ( ) for current versions shows this issues appears to be fixed as of 7.0.17 and 7.1.3
 [2017-03-30 05:26 UTC]
-Status: Open +Status: Closed -Assigned To: +Assigned To: heiglandreas
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat May 25 06:01:31 2024 UTC