How to set format in datetime-local field in CakePHP 4?

If you’re looking for consistent date formatting throughout your app, you can configure this in your bootstrap.php file. Here’s what mine looks like. I have my dates formatted MM/dd/yyyy:

// format chronos. We want 4-digit years to display throughout the app
\Cake\I18n\Time::setToStringFormat('HH:mm');
\Cake\I18n\Date::setToStringFormat('MM/dd/yyyy'); 
\Cake\I18n\FrozenTime::setToStringFormat('MM/dd/yyyy HH:mm:ss');
\Cake\I18n\FrozenDate::setToStringFormat('MM/dd/yyyy');

If I need to set a date value in a model or controller, I just use date('Y-m-d') in my code (assuming a datetime data type in your database).

1 Like