# Date conversions

**URL:** <https://discourse.cakephp.org/t/date-conversions/12230>\
**Category:** Need Help\
**Created:** [September 5, 2024, 4:31pm UTC](https://discourse.cakephp.org/t/date-conversions/12230 "2024-09-05T16:31:00Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Howan](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/howan/32/3182_2.png) [@Howan](https://discourse.cakephp.org/u/Howan)\
**Post date:** [September 5, 2024, 4:31pm UTC](https://discourse.cakephp.org/t/date-conversions/12230/1 "2024-09-05T16:31:00Z")

</div>

Hi everyone,

I save my dates in the format datetime in mysql.  
I just want to format them for display resons.

I found 2 ways. Both work. Is there a difference or a preferred one?

```auto
<td><?= h($ticket->created->i18nFormat('dd.MM.yyyy')) ?></td>
<td><?= h($ticket->created->format('d.m.Y')) ?></td>

```

Thank you!

🙏

---

<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:** [September 5, 2024, 8:41pm UTC](https://discourse.cakephp.org/t/date-conversions/12230/2 "2024-09-05T20:41:49Z")

</div>

both are fine, just one uses PHP’s default date format strings and the other uses the ICN format strings.

> **[Date & Time - 4.x](https://book.cakephp.org/4/en/core-libraries/time.html#Cake%5CI18n%5CFrozenTime::i18nFormat)**
>
> Time class helps you format time and test time.

---

<div class="post-metadata">

**Author:** ![Howan](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/howan/32/3182_2.png) [@Howan](https://discourse.cakephp.org/u/Howan)\
**Post date:** [September 5, 2024, 9:25pm UTC](https://discourse.cakephp.org/t/date-conversions/12230/3 "2024-09-05T21:25:50Z")

</div>

Perfect, and thank you for the quick reply. 👍

---

<div class="post-metadata">

**Author:** ![wasilij](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/wasilij/32/3211_2.png) [@wasilij](https://discourse.cakephp.org/u/wasilij)\
**Post date:** [September 9, 2024, 2:09am UTC](https://discourse.cakephp.org/t/date-conversions/12230/4 "2024-09-09T02:09:03Z")

</div>

If you want to apply it everywhere in the cake app, you could do this in your _AppController_:

```auto
<?php
namespace App\Controller;
use Cake\Controller\Controller;
use Cake\I18n\FrozenDate;
use Cake\I18n\FrozenTime;

class AppController extends Controller {
  public function initialize(): void {
    parent::initialize();

    // set date and time format
    FrozenDate::setToStringFormat('dd.MM.yyyy');
    FrozenTime::setToStringFormat('dd.MM.yyyy, HH:mm');

    // load components
    $this->loadComponent('Flash');
    // ..
  }

 ...
}

```

Then you do not need to do this eveytime:

```auto
<td><?= h($ticket->created->format('d.m.Y')) ?></td>

```

You can just do this:

```auto
<td><?= h($ticket->created) ?></td>

```

---

<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:** [September 9, 2024, 10:29am UTC](https://discourse.cakephp.org/t/date-conversions/12230/5 "2024-09-09T10:29:44Z")

</div>

AppController is only used in web requests, not CLI commands.

Its recommended to put these kind of `setToStringFormat()` calls into the [config/bootstrap.php](https://github.com/cakephp/app/blob/5.x/config/bootstrap.php#L223-L227) since its actually used everywhere.

---

<div class="post-metadata">

**Author:** ![wasilij](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/wasilij/32/3211_2.png) [@wasilij](https://discourse.cakephp.org/u/wasilij)\
**Post date:** [September 9, 2024, 3:57pm UTC](https://discourse.cakephp.org/t/date-conversions/12230/6 "2024-09-09T15:57:32Z")

</div>

The CLI is a good point @KevinPfeifer, thank you for the hint.  
I tried to define this settings in the bootstrap and saw, that some things with _Date_ and _Time_ have changed in version 5.  
So to define it in the _bootstrap.php_, the following is needed:

```auto
// set date and time format
Cake\I18n\Date::setToStringFormat('dd.MM.yyyy');
Cake\I18n\DateTime::setToStringFormat('dd.MM.yyyy HH:mm');

```

---

<div class="post-metadata">

**Author:** ![dereuromark](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dereuromark/32/37_2.png) [@dereuromark](https://discourse.cakephp.org/u/dereuromark)\
**Post date:** [September 11, 2024, 6:31pm UTC](https://discourse.cakephp.org/t/date-conversions/12230/7 "2024-09-11T18:31:05Z")

</div>

Inside templating i prefer to use the helpers which wrap the logic with better syntactic sugar
