Bug #72243 [NEW]: DateTimes fail to compare correctly when a DateTimeZone is set

From: Date: Thu, 19 May 2016 18:14:02 +0000
Subject: Bug #72243 [NEW]: DateTimes fail to compare correctly when a DateTimeZone is set
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-201199@lists.php.net to get a copy of this message
From: Rican7 at gmail dot com Operating system: PHP version: 7.0.6 Package: Date/time related Bug Type: Bug Bug description:DateTimes fail to compare correctly when a DateTimeZone is set Description: ------------ 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: --------------- <?php $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: ---------------- bool(true) bool(true) bool(true) bool(true) bool(true) Actual result: -------------- bool(true) bool(true) bool(false) bool(true) bool(true) -- Edit bug report at https://bugs.php.net/bug.php?id=72243&edit=1 -- Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=72243&r=trysnapshot54 Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=72243&r=trysnapshot55 Try a snapshot (trunk): https://bugs.php.net/fix.php?id=72243&r=trysnapshottrunk Fixed in SVN: https://bugs.php.net/fix.php?id=72243&r=fixed Fixed in release: https://bugs.php.net/fix.php?id=72243&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=72243&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=72243&r=needscript Try newer version: https://bugs.php.net/fix.php?id=72243&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=72243&r=support Expected behavior: https://bugs.php.net/fix.php?id=72243&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=72243&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=72243&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=72243&r=globals PHP 4 support discontinued: https://bugs.php.net/fix.php?id=72243&r=php4 Daylight Savings: https://bugs.php.net/fix.php?id=72243&r=dst IIS Stability: https://bugs.php.net/fix.php?id=72243&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=72243&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=72243&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=72243&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=72243&r=mysqlcfg

« previous php.bugs (#201199) next »