# Date format version 3.3

**URL:** <https://discourse.cakephp.org/t/date-format-version-3-3/1079>\
**Category:** Need Help\
**Created:** [August 31, 2016, 3:58pm UTC](https://discourse.cakephp.org/t/date-format-version-3-3/1079 "2016-08-31T15:58:52Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![jimgwhit](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/jimgwhit/32/378_2.png) [@jimgwhit](https://discourse.cakephp.org/u/jimgwhit)\
**Post date:** [August 31, 2016, 3:58pm UTC](https://discourse.cakephp.org/t/date-format-version-3-3/1079/1 "2016-08-31T15:58:52Z")

</div>

In cakephp 3.3 is there a way to set date format somewhere to yyyy-mm-dd the mysql standard? I have searched, and dug around in docs and config files.

---

<div class="post-metadata">

**Author:** ![markstory](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/markstory/32/182_2.png) [@markstory](https://discourse.cakephp.org/u/markstory)\
**Post date:** [September 1, 2016, 2:48am UTC](https://discourse.cakephp.org/t/date-format-version-3-3/1079/2 "2016-09-01T02:48:16Z")

</div>

Set it where for what output? Have you looked at `Time::setToStringFormat()` ?

---

<div class="post-metadata">

**Author:** ![jimgwhit](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/jimgwhit/32/378_2.png) [@jimgwhit](https://discourse.cakephp.org/u/jimgwhit)\
**Post date:** [September 23, 2016, 4:09am UTC](https://discourse.cakephp.org/t/date-format-version-3-3/1079/3 "2016-09-23T04:09:02Z")

</div>

> Set it where for what output? Have you looked at Time::setToStringFormat() ?

Having come from java technologies many years ago, then php, then mvc then laravel, and after deciding laravel has gotton out of hand I figured I would try cakephp, it’s been around and is / should be stable. Next I decide to try the forum. I know of no other way to ask such a simple question.

> Set it where

Config

> for what output

yyyy-mm-dd as stated.

I wound up having to

```auto
date_format($row->lastedit, 'Y-m-d H:i:s') 

```

Now it would seem to me a framework as old and stable as cake would have a config setting to set how a date is to be displayed.

---

<div class="post-metadata">

**Author:** ![Graziel](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@Graziel](https://discourse.cakephp.org/u/Graziel)\
**Post date:** [September 23, 2016, 4:32pm UTC](https://discourse.cakephp.org/t/date-format-version-3-3/1079/4 "2016-09-23T16:32:12Z")

</div>

`Time::setToStringFormat()` is a static method so you can call it anywhere, `bootstrap.php` for instance or in your `AppController::beforeFilter` if you have multi language site

[http://api.cakephp.org/3.3/class-Cake.I18n.DateFormatTrait.html#\_setToStringFormat](http://api.cakephp.org/3.3/class-Cake.I18n.DateFormatTrait.html#_setToStringFormat)  
[http://book.cakephp.org/3.0/en/core-libraries/time.html#setting-the-default-locale-and-format-string](http://book.cakephp.org/3.0/en/core-libraries/time.html#setting-the-default-locale-and-format-string)

just note it uses ICU format to format

[http://book.cakephp.org/3.0/en/core-libraries/time.html#Cake\I18n\Time::i18nFormat](http://book.cakephp.org/3.0/en/core-libraries/time.html#Cake%5CI18n%5CTime::i18nFormat)

---

<div class="post-metadata">

**Author:** ![jimgwhit](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/jimgwhit/32/378_2.png) [@jimgwhit](https://discourse.cakephp.org/u/jimgwhit)\
**Post date:** [September 23, 2016, 7:48pm UTC](https://discourse.cakephp.org/t/date-format-version-3-3/1079/5 "2016-09-23T19:48:08Z")

</div>

From the api

```auto
setToStringFormat( string|array|integer $format )

```

That doesn’t tell me what to put for

- string
- array
- I am guessing integer $format would be ‘Y-m-d H:i:s’

Why is it so hard in cakephp just to globally default to: Y-m-d H:i:s or Y-m-d if no time needed.  
Or what format someone wants to set, why a silly class.

As another person stated, the docs can really leave you hanging, as there aren’t clear concise examples of some stuff.

---

<div class="post-metadata">

**Author:** ![Graziel](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@Graziel](https://discourse.cakephp.org/u/Graziel)\
**Post date:** [September 23, 2016, 8:02pm UTC](https://discourse.cakephp.org/t/date-format-version-3-3/1079/6 "2016-09-23T20:02:46Z")

</div>

> [@jimgwhit](#):
>
> That doesn’t tell me what to put for

i see you didnt bother to check 2nd link

> **[Date & Time - 3.10](https://book.cakephp.org/3/en/core-libraries/time.html)**
>
> Time class helps you format time and test time.

which gives you examples

```php
FrozenDate::setToStringFormat(\IntlDateFormatter::SHORT); // For any immutable Date

// The same method exists on Date, FrozenDate and FrozenTime
Time::setToStringFormat([
    \IntlDateFormatter::FULL,
    \IntlDateFormatter::SHORT
]);

// The same method exists on Date, FrozenDate and FrozenTime
Time::setToStringFormat('yyyy-MM-dd HH:mm:ss');

```

> [@jimgwhit](#):
>
> Why is it so hard in cakephp just to globally default to: Y-m-d H:i:s or Y-m-d

in bootstrap.php add  
`Time::setToStringFormat('yyyy-MM-dd HH:mm:ss');`  
which again is in link i gave, then in view you just do  
`<?= $row->lastedit ?>` thats it and how hard was it?

---

<div class="post-metadata">

**Author:** ![jimgwhit](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/jimgwhit/32/378_2.png) [@jimgwhit](https://discourse.cakephp.org/u/jimgwhit)\
**Post date:** [September 23, 2016, 9:03pm UTC](https://discourse.cakephp.org/t/date-format-version-3-3/1079/7 "2016-09-23T21:03:40Z")

</div>

Putting

```auto
Time::setToStringFormat('yyyy-MM-dd HH:mm:ss');

```

only gives error

```auto
Error: Class 'Time' not found
File C:\Bitnami\wampstack-5.6.22-0\apache2\htdocs\cakephp\config\bootstrap.php
Line: 227 

```

This so called date time stuff in cake is a mess.

---

<div class="post-metadata">

**Author:** ![Graziel](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@Graziel](https://discourse.cakephp.org/u/Graziel)\
**Post date:** [September 23, 2016, 9:33pm UTC](https://discourse.cakephp.org/t/date-format-version-3-3/1079/8 "2016-09-23T21:33:30Z")

</div>

you need to add path to the class otherwise PHP doesnt know which `Time` class you want to use, is it `Cake\I18n\Time` or some external lib class?  
you can add `use Cake\I18n\Time;` to top of for instance `bootstrap.php` or `Cake\I18n\Time::setToStringFormat` anywhere, its not Cake problem [http://php.net/manual/en/language.namespaces.php](http://php.net/manual/en/language.namespaces.php)

---

<div class="post-metadata">

**Author:** ![jimgwhit](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/jimgwhit/32/378_2.png) [@jimgwhit](https://discourse.cakephp.org/u/jimgwhit)\
**Post date:** [September 23, 2016, 10:41pm UTC](https://discourse.cakephp.org/t/date-format-version-3-3/1079/9 "2016-09-23T22:41:16Z")

</div>

I added the use statement, now no error but still displaying like

```auto
9/23/16, 4:05 PM

```

I want

```auto
2016-09-23 16:05:48

```

---

<div class="post-metadata">

**Author:** ![Graziel](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@Graziel](https://discourse.cakephp.org/u/Graziel)\
**Post date:** [September 23, 2016, 10:56pm UTC](https://discourse.cakephp.org/t/date-format-version-3-3/1079/10 "2016-09-23T22:56:17Z")

</div>

beyond that you need to supply some code cause it works for me

---

<div class="post-metadata">

**Author:** ![jimgwhit](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/jimgwhit/32/378_2.png) [@jimgwhit](https://discourse.cakephp.org/u/jimgwhit)\
**Post date:** [September 23, 2016, 11:02pm UTC](https://discourse.cakephp.org/t/date-format-version-3-3/1079/11 "2016-09-23T23:02:08Z")

</div>

bootstrap

```auto
use Cake\Network\Request;
use Cake\Utility\Inflector;
use Cake\Utility\Security;
use Cake\I18n\Time;

```

Bottom of bootstrap

```auto
Time::setToStringFormat('yyyy-MM-dd HH:mm:ss');

```

view

```auto
echo "<td style=\"width:40px;\">" . $row->lastedit . "</td>";

```

If however I do this

```auto
echo "<td style=\"width:40px;\">" . date_format($row->lastedit, 'Y-m-d H:i:s') . "</td>";

```

It is formatted correctly.

---

<div class="post-metadata">

**Author:** ![Graziel](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@Graziel](https://discourse.cakephp.org/u/Graziel)\
**Post date:** [September 24, 2016, 12:11am UTC](https://discourse.cakephp.org/t/date-format-version-3-3/1079/12 "2016-09-24T00:11:22Z")

</div>

and what type is `$row->lastedit`?

---

<div class="post-metadata">

**Author:** ![jimgwhit](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/jimgwhit/32/378_2.png) [@jimgwhit](https://discourse.cakephp.org/u/jimgwhit)\
**Post date:** [September 24, 2016, 1:37am UTC](https://discourse.cakephp.org/t/date-format-version-3-3/1079/13 "2016-09-24T01:37:43Z")

</div>

It is a datetime field.

---

<div class="post-metadata">

**Author:** ![Graziel](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@Graziel](https://discourse.cakephp.org/u/Graziel)\
**Post date:** [September 27, 2016, 2:08am UTC](https://discourse.cakephp.org/t/date-format-version-3-3/1079/14 "2016-09-27T02:08:54Z")

</div>

then something is changing your output beetween bootstrap.php and your view code

---

<div class="post-metadata">

**Author:** ![Diego](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/diego/32/296_2.png) [@Diego](https://discourse.cakephp.org/u/Diego)\
**Post date:** [September 27, 2016, 12:20pm UTC](https://discourse.cakephp.org/t/date-format-version-3-3/1079/15 "2016-09-27T12:20:51Z")

</div>

```
\Cake\I18n\Time::setToStringFormat('Y-m-d H:i:s');
\Cake\I18n\Date::setToStringFormat('Y-m-d');
\Cake\I18n\FrozenTime::setToStringFormat('Y-m-d H:i:s');
\Cake\I18n\FrozenDate::setToStringFormat('Y-m-d');

```

this maybe help

> [@CakePHP Locale pt\_BR and DateFormat](http://discourse.cakephp.org/t/cakephp-locale-pt-br-and-dateformat/1002):
>
> Hi ! I am trying to localize my Cake 3.3 app to portuguese “Brasil”. I’ve downloaded [this](https://raw.githubusercontent.com/jrbasso/cake_ptbr/master/src/Locale/pt_BR/cake.po) and saved in C:\Apache24\htdocs\cake3\src\Locale\pt\_BR I’ve changed the defaultLocale in C:\Apache24\htdocs\cake3\config\app.php to ‘defaultLocale’ =\> env(‘APP\_DEFAULT\_LOCALE’, ‘pt\_BR’) But Cake is still in english and I have tried to change the date format in C:\Apache24\htdocs\cake3\config\bootstrap.php with: // Configure a custom datetime format parser format. Type::build('date') -\>useLocaleParser…

---

<div class="post-metadata">

**Author:** ![jimgwhit](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/jimgwhit/32/378_2.png) [@jimgwhit](https://discourse.cakephp.org/u/jimgwhit)\
**Post date:** [September 27, 2016, 7:55pm UTC](https://discourse.cakephp.org/t/date-format-version-3-3/1079/16 "2016-09-27T19:55:25Z")

</div>

@Diego thanks for reply, I wound up just doing

```auto
echo "<td style=\"width:40px;\">" . $row->lastedit->toDateTimeString() . "</td>";

```

Found it tucked away in a trait. Wasn’t in the docs, I created an issue and they are adding it to docs. I was hoping there was a global setting to change the format to Y-m-d H:i:s for datetime, but I guess there is not a config setting for it. Naturally I could always format with php like:

```auto
echo "<td style=\"width:40px;\">" . date_format($row->lastedit, 'Y-m-d H:i:s') . "</td>";

```

But since I am using cake I figured I’d try their way.

See [Chronos FormattingTrait](http://discourse.cakephp.org/t/chronos-formattingtrait/1239/2)
