# DateTime object equality

- Thread: https://externals.io/message/28230
- Original archive: https://news-web.php.net/php.internals/28230
- Started by Hans Lellelid on 2007-03-04T20:52:16+00:00
- Messages: 5
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #28230 — Hans Lellelid — 2007-03-04T20:52:16+00:00

URL: https://externals.io/message/28230#28230

Hi all,

DateTime equality (not identity) appears to be broken.  I've created a
ticket for this issue (http://bugs.php.net/bug.php?id=40691), which
keeps getting marked as bogus by Ilia.  This may be a more appropriate
issue to raise on list, as I seem to be having both a very hard time
making my point about the inconsistency of the behavior and a very hard
time being convinced that this is not a bug (at *least* a documentation
problem, if nothing else) :)

The basic problem is that a [non-identity] comparison (==) of *any*
DateTime object with any other DateTime object will return TRUE.  Now, I
admit that I know little about the underlying layer, but as a developer
the DateTime object has a very clear set of properties (namely
date/time, maybe time zone) that I would expect would be compared by a
== equality check.

$d1 = new DateTime("2001-01-01");
$d2 = new DateTime("2007-02-28");
var_export($d1 == $d2); // Ouputs: `TRUE` 


This always-return-TRUE behavior is extremely counter-intuitive.  It
also appears to be very exceptional in the PHP core classes.
Admittedly, I haven't looked through SPL, but the Exception object
provides a perfect example for what I would expect (based on the text in
the PHP manual about object comparison):

$a = new Exception("foo");
$b = new Exception("bar");
$c = new Exception("foo");

var_export($a == $b); // Outputs: `FALSE` 
var_export($a == $c); // Outputs: `TRUE` 

Obviously stdClass and user-defined classes exhibit this same behavior
of testing the object properties -- just as I'd expect from the
description in the manual.

Is this DateTime comparison behavior actually intended to be different
from everything else?  If there's some reason that DateTime object
properties cannot be compared to each other, wouldn't it be more
appropriate for them to always return `FALSE` ?

Thanks,
Hans

---

## #28237 — Richard Quadling — 2007-03-05T11:51:36+00:00

URL: https://externals.io/message/28230#28237 · In reply to: #28230

It would be interesting to know what other internal classes falsely
pass this non-identity comparison.

Then two new documentation entities could be used to say that
non-identity comparison is or is not possible with this class.

In my installation, I have 95 built in classes (with no extensions
added), or 106 with the extensions I use normally. I managed to get
upto 198 built in classes (by adding ALL the extensions I could get
working).

I suppose the step after this would be to make the built in classes
operate so that non-identity comparison operated based upon the USER's
perception of what should happen.




> Hi all,
>
> DateTime equality (not identity) appears to be broken.  I've created a
> ticket for this issue (http://bugs.php.net/bug.php?id=40691), which
> keeps getting marked as bogus by Ilia.  This may be a more appropriate
> issue to raise on list, as I seem to be having both a very hard time
> making my point about the inconsistency of the behavior and a very hard
> time being convinced that this is not a bug (at *least* a documentation
> problem, if nothing else) :)
>
> The basic problem is that a [non-identity] comparison (==) of *any*
> DateTime object with any other DateTime object will return TRUE.  Now, I
> admit that I know little about the underlying layer, but as a developer
> the DateTime object has a very clear set of properties (namely
> date/time, maybe time zone) that I would expect would be compared by a
> == equality check.
>
> $d1 = new DateTime("2001-01-01");
> $d2 = new DateTime("2007-02-28");
> var_export($d1 == $d2); // Ouputs: `TRUE` 
>
>
> This always-return-TRUE behavior is extremely counter-intuitive.  It
> also appears to be very exceptional in the PHP core classes.
> Admittedly, I haven't looked through SPL, but the Exception object
> provides a perfect example for what I would expect (based on the text in
> the PHP manual about object comparison):
>
> $a = new Exception("foo");
> $b = new Exception("bar");
> $c = new Exception("foo");
>
> var_export($a == $b); // Outputs: `FALSE` 
> var_export($a == $c); // Outputs: `TRUE` 
>
> Obviously stdClass and user-defined classes exhibit this same behavior
> of testing the object properties -- just as I'd expect from the
> description in the manual.
>
> Is this DateTime comparison behavior actually intended to be different
> from everything else?  If there's some reason that DateTime object
> properties cannot be compared to each other, wouldn't it be more
> appropriate for them to always return `FALSE` ?
>
> Thanks,
> Hans
>
> --
> 
> 
>
>


-- 
-----
Richard Quadling
Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
"Standing on the shoulders of some very clever giants!"

---

## #28241 — Michael Wallner — 2007-03-05T13:23:30+00:00

URL: https://externals.io/message/28230#28241 · In reply to: #28230

Hans Lellelid wrote:

> Is this DateTime comparison behavior actually intended to be different
> from everything else?  If there's some reason that DateTime object
> properties cannot be compared to each other, wouldn't it be more
> appropriate for them to always return `FALSE` ?

In my POV this is easily solvable with a custom comparison object handler.
See attached patch.  I also think that comparing to Datetime instances should
give a reasonable result.

Regards,
-- 
Michael

---

## #28243 — Michael Wallner — 2007-03-05T13:51:03+00:00

URL: https://externals.io/message/28230#28243 · In reply to: #28241

Michael Wallner wrote:
> Hans Lellelid wrote:
> 
> > […]
> 
> In my POV this is easily solvable with a custom comparison object handler.
> See attached patch.  I also think that comparing to Datetime instances should
> give a reasonable result.
> 
> Regards,


-- 
Michael

---

## #28244 — Michael Wallner — 2007-03-05T13:52:36+00:00

URL: https://externals.io/message/28230#28244 · In reply to: #28243

Michael Wallner wrote:

> +		if (!o2->time->sse_uptodate) {
> +			timelib_update_ts(o1->time, o1->time->tz_info);
> +		}

Oh my, typo warning.

-- 
Michael
