#40691 [Opn->Bgs]: Incorrect results of DateTime equality check
| From: | iliaa@php.net | Date: | Sun, 04 Mar 2007 18:34:12 +0000 |
| Subject: | #40691 [Opn->Bgs]: Incorrect results of DateTime equality check | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-109875@lists.php.net to get a copy of this message | ||
ID: 40691
Updated by: iliaa@php.net
Reported By: hans at velum dot net
-Status: Open
+Status: Bogus
Bug Type: Date/time related
Operating System: Gentoo Linux
PHP Version: 5.2.1
New Comment:
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to
report
a bug at http://bugs.php.net/how-to-report.php
This is not a bug. The date extension does not provide (or is intended)
for the purpose of comparing two date objects. You could have the same
date in the object by different properties initialized due to the way
the object was created.
As I've said earlier the most reliable way to compare two dates would
be
to convert them to unix timestamps and then compare the two.
Previous Comments:
------------------------------------------------------------------------
[2007-03-03 21:36:07] dzuelke at gmail dot com
hans at velum dot net is correct, I just stumbled over the same issue.
It definitely smells like a bug, nothing bogus here. The stored date is
a property of the object, but not compared properly as it should
according to the rules described at http://php.net/manual/en/
language.oop5.php. Probably because it's not possible to pull the
individual parts of a date (day, month, year etc) from a DateTime
instance, but that's a different story...
------------------------------------------------------------------------
[2007-03-03 19:47:15] hans at velum dot net
This is not bogus. Maybe "won't fix" but definitely not bogus.
Please note that I am quite familiar with object comparison in PHP. The
documenation says: "When using the comparison operator (==), object
variables are compared in a simple manner, namely: Two object instances
are equal if they have the same attributes and values, and are instances
of the same class."
Indeed, AS I POINTED OUT IN THE DESCRIPTION, other built-in objects in
PHP demonstrate the correct/expected behavior:
$a = new Exception("foo");
$b = new Exception("bar");
$c = new Exception("foo");
var_export($a == $b); // Outputs: FALSE
var_export($a == $c); // Outputs: TRUE
A DateTime object have very obvious properties (namely the date/time
value contained, possibly time zone of other info specified). The
equality check (NOT IDENTITY CHECK) should be comparing those values,
as apparently it does for Exception.
------------------------------------------------------------------------
[2007-03-03 15:55:40] iliaa@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to
report
a bug at http://bugs.php.net/how-to-report.php
Read on object comparison in PHP, what you are attempting will not
work.
if you want to compare 2 dates, convert them to unix timestamps first.
------------------------------------------------------------------------
[2007-03-02 15:43:14] hans at velum dot net
Description:
------------
The equality check (==) for DateTime objects does not actually check
the properties of the object (i.e. the internally stored date). This
is very counter-intuitive as it does not follow the behavior of
user-created objects or even other internal PHP objects like Exception.
Reproduce code:
---------------
$d1 = new DateTime("2001-01-01");
$d2 = new DateTime("2007-02-28");
print "DateTime Equal? " . var_export($d1 == $d2, true) . "\n";
Expected result:
----------------
DateTime Equal? false
Actual result:
--------------
DateTime Equal? true
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=40691&edit=1