In my CakePHP bootstrap.php I have the following:
use Cake\I18n\Date;
use Cake\I18n\FrozenDate;
use Cake\I18n\FrozenTime;
use Cake\I18n\Time;
Time::setJsonEncodeFormat('yyyy-MM-dd HH:mm:ss');
FrozenTime::setJsonEncodeFormat('yyyy-MM-dd HH:mm:ss');
Date::setJsonEncodeFormat('yyyy-MM-dd HH:mm:ss');
FrozenDate::setJsonEncodeFormat('yyyy-MM-dd HH:mm:ss');
When in an API method I do the following:
use Cake\I18n\Time;
$time = new Time();
$this->set([
'time' => $time,
'_serialize' => ['time']
]);
The result is something like this:
{
"time": "2017-05-16 11.55.13"
}
Note the incorrect time format. For some reason CakePHP/PHP 7/Linux changes the :
to a .
. On Windows it works fine.
CakePHP version => 3.4.6
(although this problem has been there a long time)
PHP Version => 7.0.15-0ubuntu0.16.04.4
The correct answer as provided by @ndm:
At least in ICU versions prior to 56.1, the correct way to use setJsonEncodeFormat is to escape the colon with '
like so:
Time::setJsonEncodeFormat("yyyy-MM-dd HH':'mm':'ss");
FrozenTime::setJsonEncodeFormat("yyyy-MM-dd HH':'mm':'ss");
Date::setJsonEncodeFormat("yyyy-MM-dd HH':'mm':'ss");
FrozenDate::setJsonEncodeFormat("yyyy-MM-dd HH':'mm':'ss");