|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2007-03-02 15:43 UTC] 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
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Dec 03 07:00:01 2025 UTC |
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.