beerLantern beerLantern - 1 year ago 186
PHP Question

Changing default datetime format in Cakephp 3

I am using Cake Crud Api plugin and baking all models.

The datetime format in the json response is like this:

"created": "2016-08-01T08:49:11+0000"

I want it to look like a normal datatime:

"created": "2016-08-01 08:49:11"

I have tryied setting application wide:

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

with no luck and also I have searched for any Time reference in the Crud plugin, but I haven't finded out where the default ajax format comes from.

Any idea? Thanks.

Answer Source

If someone has the same problem just add this to your app controller:

public function initialize()

    Time::setJsonEncodeFormat('yyyy-MM-dd HH:mm:ss');  // For any mutable DateTime
    FrozenTime::setJsonEncodeFormat('yyyy-MM-dd HH:mm:ss');  // For any immutable DateTime
    Date::setJsonEncodeFormat('yyyy-MM-dd HH:mm:ss');  // For any mutable Date
    FrozenDate::setJsonEncodeFormat('yyyy-MM-dd HH:mm:ss');  // For any immutable Date

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download