Incorrect date value when patch entity CakePHP 4

I try to save data from a cakephp 4 form. All data are well saved but date not. I’ve got some date fields. Those fields are filled by bootstrap datepicker widget and are in the d/m/Y format.

The problem seems to happened when pacthing entity.

  • Debug of $this->request->getData();
[
	...
	'departure_day' => '06/01/2021'
	...
]
  • Debug of patched entity;
object(App\Model\Entity\Travel) id:0 {
	'id' => (int) 2
	'departure_day' => object(Cake\I18n\FrozenDate) id:1 {
		'time' => '2021-01-05 00:00:00.000000-03:00'
		'timezone' => 'America/Sao_Paulo'
		'fixedNowTime' => false
		date => '2021-01-05 00:00:00.000000'
		timezone_type => (int) 3
		timezone => 'America/Sao_Paulo'
		...
	}
	...
}

the entity date is different from the sent date

But before that, I set expected date format with:

TypeFactory::build('date')->useLocaleParser()->setLocaleFormat('dd/MM/yyyy');

Anyone have idea of what that’s wrong?
Thanks a lot.

There is nothing obviously wrong… Is it possible that the variable used to set departure_day does not hold the value you think? Have you debugged it before using it to confirm?

Whats your configured App.defaultTimezone and maybe Datasources.default.timezone (check you config/app.php)

The time zone of both is America/Sao_Paulo (-03:00)
I performed some tests with other time zones and the problem did not happen :frowning:

TypeFactory::build(‘date’)->useLocaleParser()->setLocaleFormat(‘dd/MM/yyyy’);

You format string is incorrect. It needs to be dd/LL/yyyy.

I performed a test with the suggested changes and it didn’t work either :frowning:

I think you can try this following code

 'defaultLocale' => env('APP_DEFAULT_LOCALE', 'en_US'),
 'defaultTimezone' => env('APP_DEFAULT_TIMEZONE', 'UTC'),

I guess the entity wasn’t patched properly and the field departure_day didn’t change at all from original.

Check departure_day is accessible in :

  1. src/Model/Entity/Travel.php (protected $_accessible ...)
  2. when you patch the data. Use fields as below
$entity = $this->patchEntity($entity, $data, [
    'fields' => ['departure_day']
]);