Bug #68474 [NEW]: DateTime comparison fails

From: Date: Fri, 21 Nov 2014 19:04:22 +0000
Subject: Bug #68474 [NEW]: DateTime comparison fails
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-188736@lists.php.net to get a copy of this message
From: cwente at ip dot ulagos dot cl Operating system: Debian Linux 7.7 PHP version: 5.5.19 Package: Date/time related Bug Type: Bug Bug description:DateTime comparison fails Description: ------------ When creating a DateTime from the WTC representation and then copying the DateTimeZone the comparison (<,>,==) fails. In the example script PHP will say that 2014-11-21 18:00:00 == 2014-11-21 15:00:00 both with DateTimeZone -03:00, which is clearly wrong. Test script: --------------- $a = new DateTime("2014-11-21 15:00:00", new DateTimeZone("America/Santiago")); echo "------------------\nCopying \$b from W3C representation of \$a\n"; $b = new DateTime($a->format(DateTime::W3C)); compare($a, $b); $a->add(new DateInterval("PT3H")); echo "------------------\nAdd 3 hours to \$a\n"; compare($a, $b); $a->setTimezone($b->getTimezone()); echo "------------------\nSet Timezone of \$a to Timezone of \$b\n"; compare($a, $b); echo "------------------\nCompare Timestamps:\n"; compare($a->getTimestamp(), $b->getTimestamp()); function compare($a, $b) { var_dump($a); var_dump($b); echo ($a > $b ? '$a > $b' : ($a < $b ? '$a < $b' : ($a == $b ? '$a == $b' : 'undefined'))) . "\n"; } Expected result: ---------------- The time in the future should be greater than the time in the past, if the timezone is the same. Actual result: -------------- ------------------ Copying $b from W3C representation of $a class DateTime#1 (3) { public $date => string(26) "2014-11-21 15:00:00.000000" public $timezone_type => int(3) public $timezone => string(16) "America/Santiago" } class DateTime#2 (3) { public $date => string(26) "2014-11-21 15:00:00.000000" public $timezone_type => int(1) public $timezone => string(6) "-03:00" } $a == $b ------------------ Add 3 hours to $a class DateTime#1 (3) { public $date => string(26) "2014-11-21 18:00:00.000000" public $timezone_type => int(3) public $timezone => string(16) "America/Santiago" } class DateTime#2 (3) { public $date => string(26) "2014-11-21 15:00:00.000000" public $timezone_type => int(1) public $timezone => string(6) "-03:00" } $a > $b ------------------ Set Timezone of $a to Timezone of $b class DateTime#1 (3) { public $date => string(26) "2014-11-21 18:00:00.000000" public $timezone_type => int(1) public $timezone => string(6) "-03:00" } class DateTime#2 (3) { public $date => string(26) "2014-11-21 15:00:00.000000" public $timezone_type => int(1) public $timezone => string(6) "-03:00" } $a == $b ------------------ Compare Timestamps: int(1416603600) int(1416592800) $a > $b -- Edit bug report at https://bugs.php.net/bug.php?id=68474&edit=1 -- Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=68474&r=trysnapshot54 Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=68474&r=trysnapshot55 Try a snapshot (trunk): https://bugs.php.net/fix.php?id=68474&r=trysnapshottrunk Fixed in SVN: https://bugs.php.net/fix.php?id=68474&r=fixed Fixed in release: https://bugs.php.net/fix.php?id=68474&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=68474&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=68474&r=needscript Try newer version: https://bugs.php.net/fix.php?id=68474&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=68474&r=support Expected behavior: https://bugs.php.net/fix.php?id=68474&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=68474&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=68474&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=68474&r=globals PHP 4 support discontinued: https://bugs.php.net/fix.php?id=68474&r=php4 Daylight Savings: https://bugs.php.net/fix.php?id=68474&r=dst IIS Stability: https://bugs.php.net/fix.php?id=68474&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=68474&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=68474&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=68474&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=68474&r=mysqlcfg

« previous php.bugs (#188736) next »