# Comparing Date with DateTime in Cake 5

**URL:** <https://discourse.cakephp.org/t/comparing-date-with-datetime-in-cake-5/12166>\
**Category:** Uncategorized\
**Tags:** behavior\
**Created:** [August 5, 2024, 3:09pm UTC](https://discourse.cakephp.org/t/comparing-date-with-datetime-in-cake-5/12166 "2024-08-05T15:09:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kez](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/kez/32/3156_2.png) [@Kez](https://discourse.cakephp.org/u/Kez)\
**Post date:** [August 5, 2024, 3:09pm UTC](https://discourse.cakephp.org/t/comparing-date-with-datetime-in-cake-5/12166/1 "2024-08-05T15:09:14Z")

</div>

Hi,

Recently migrated my app to Cake 5 - I’ve run into issues in my existing code when comparing two dates, where suddenly the result is always false. For example `$date1 < $date2`.

After some digging it seems like that cause of this new behaviour is I am comparing `Cake\I18N\Date` objects with `Cake\I18N\DateTime` objects. Previously I believe I was using FrozenDate\FrozenTime and the migrations script automatically replaced them.

In the [cake5 documentation](https://book.cakephp.org/chronos/3/en/index.html#date-objects) it says that Date objects have a fixed time of `00:00:00` - so one would think that you could compare these two objects simply?

I was wondering if I others are able to replicate this behaviour and, if so, is it the correct/desired behaviour in CakePHP 5? Perhaps I should avoid using comparison operators altogether and instead use the objects comparison methods?

Thanks!

---

<div class="post-metadata">

**Author:** ![KevinPfeifer](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/kevinpfeifer/32/3365_2.png) [@KevinPfeifer](https://discourse.cakephp.org/u/KevinPfeifer)\
**Post date:** [August 5, 2024, 3:23pm UTC](https://discourse.cakephp.org/t/comparing-date-with-datetime-in-cake-5/12166/2 "2024-08-05T15:23:32Z")

</div>

I do agree that this is weird…

```auto
$yesterday = new \Cake\I18n\Date('yesterday');
$now = new \Cake\I18n\DateTime('now');

$this->assertTrue($yesterday->toNative() < $now->toNative());
$this->assertTrue($yesterday < $now);

```

the second assertion fails but the first works

---

<div class="post-metadata">

**Author:** ![KevinPfeifer](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/kevinpfeifer/32/3365_2.png) [@KevinPfeifer](https://discourse.cakephp.org/u/KevinPfeifer)\
**Post date:** [August 5, 2024, 3:34pm UTC](https://discourse.cakephp.org/t/comparing-date-with-datetime-in-cake-5/12166/3 "2024-08-05T15:34:41Z")

</div>

well… as always its a bit more complicated.

The reason why this this is not working anymore since CakePHP 5 (and more specifically since Chronos 3) is the fact, that Date objects and DateTime Objects don’t share the same `DateTimeImmutable` class anymore.

See [Release Chronos 3.0.2 · cakephp/chronos · GitHub](https://github.com/cakephp/chronos/releases/tag/3.0.2) as well as [Reasoning around no longer extending native DateTimeImmutable · Issue #410 · cakephp/chronos · GitHub](https://github.com/cakephp/chronos/issues/410#issuecomment-1722185717)

So your best course would be to call `->toNative()` to both instances since then you will always get a `DateTimeImmutable` object.

---

<div class="post-metadata">

**Author:** ![Kez](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/kez/32/3156_2.png) [@Kez](https://discourse.cakephp.org/u/Kez)\
**Post date:** [August 7, 2024, 8:32am UTC](https://discourse.cakephp.org/t/comparing-date-with-datetime-in-cake-5/12166/4 "2024-08-07T08:32:02Z")

</div>

Okay, makes sense. Thanks! It may be worth spelling this out in the migration guide - it’s quite difficult to spot since it doesn’t throw any errors.

---

<div class="post-metadata">

**Author:** ![KevinPfeifer](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/kevinpfeifer/32/3365_2.png) [@KevinPfeifer](https://discourse.cakephp.org/u/KevinPfeifer)\
**Post date:** [August 7, 2024, 10:14am UTC](https://discourse.cakephp.org/t/comparing-date-with-datetime-in-cake-5/12166/5 "2024-08-07T10:14:14Z")

</div>

Well, it’s kind of mentioned in the docs as you can see here:  
[https://book.cakephp.org/5/en/appendices/5-0-migration-guide.html#database](https://book.cakephp.org/5/en/appendices/5-0-migration-guide.html#database)

Sure, it doesn’t exactly mention this specific problem but a special notice would definitely help.
