Bug #68474 [Com]: DateTime comparison fails
| From: | dana dot desrosiers at gmail dot com | Date: | Sat, 31 Jan 2015 02:37:16 +0000 |
| Subject: | Bug #68474 [Com]: DateTime comparison fails | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-190362@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=68474&edit=1
ID: 68474
Comment by: dana dot desrosiers at gmail dot com
Reported by: cwente at ip dot ulagos dot cl
Summary: DateTime comparison fails
Status: Open
Type: Bug
Package: Date/time related
Operating System: Debian Linux 7.7
PHP Version: 5.5.19
Block user comment: N
Private report: N
New Comment:
I ran into this problem in PHP 5.6.3 as well. This is what I think is happening:
The DateTime object has a timestamp property that is used to compare two DateTime objects using
comparison operators (<, >, ==, etc.). When the setTimezone() method is called, the timezone
property is set, but the internal timestamp is never updated, so comparisons may be wrong.
Calling getTimestamp() or modify() after setTimezone() corrects the error.
This bug does not exist in 5.3.27.
Previous Comments:
------------------------------------------------------------------------
[2014-11-21 19:04:20] cwente at ip dot ulagos dot cl
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 this bug report at https://bugs.php.net/bug.php?id=68474&edit=1