Date format version 3.3

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.

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

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


for what output

yyyy-mm-dd as stated.

I wound up having to

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.

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

just note it uses ICU format to format\I18n\Time::i18nFormat

From the api

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.

i see you didnt bother to check 2nd link
which gives you examples

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

// The same method exists on Date, FrozenDate and FrozenTime

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

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?


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

only gives error

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.

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

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

9/23/16, 4:05 PM

I want

2016-09-23 16:05:48

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


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

Bottom of bootstrap

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


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

If however I do this

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

It is formatted correctly.

and what type is $row->lastedit?

It is a datetime field.

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

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

this maybe help

@Diego thanks for reply, I wound up just doing

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:

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