Struggling with date / time formats

hello there, its me again.

since days i am struggling hard with parsing, fetching and inserting dates or times.
My current setup:

bootstrap.php

ini_set('intl.default_locale', 'de_DE');
Time::setDefaultLocale('de-DE'); // For any mutable DateTime
FrozenTime::setDefaultLocale('de-DE'); // For any immutable DateTime
Date::setDefaultLocale('de-DE');
FrozenDate::setDefaultLocale('de-DE');

Date::setToStringFormat('d.MM.Y');
FrozenDate::setToStringFormat('d.MM.Y');
FrozenTime::setToStringFormat('H:i');
Time::setToStringFormat('H:i');

Type::build('datetime')->useLocaleParser();
Type::build('date')->useLocaleParser();
Type::build('time')->useLocaleParser();

i found some of these on stackoverflow and cookbook.

Inserting dates or times using time- / datepicker component of jquery-ui in combination with this configuration is working properly:

function initDateTimePicker() {
$(".datepicker").datepicker({
    dateFormat: 'dd.mm.yy'
});

$(".datepicker").datepicker("option", "dateFormat", "dd.mm.yy");

$(".timepicker").timepicker({
    show2400: true,
    timeFormat: 'H:i'
});

$(".timepicker").timepicker("option", "timeFormat", "H:i");

}

Loading values of time datatypes is not working properly … see comparison of database values and output in form:

database:

output

where “Von” is the left-hand column in the database screenshot and “Bis” the right-hand column.
DB: 00:00 - 00:30, Output: 00:00-00:00

Another thing is, when updating an entry with its associations I have to parse date_create values as hidden input fields because they are mandatory and flagged as NOT NULL in database.

Form Template:

 echo $this->Form->hidden('channel_opening_hours.3.date_create', ['value' => $channel->channel_opening_hours[3]->date_create]);
            echo $this->Form->hidden('channel_opening_hours.3.date_change', ['value' => $channel->channel_opening_hours[3]->date_change]);


            echo $this->Form->hidden('channel_opening_hours.4.date_create', ['value' => $channel->channel_opening_hours[4]->date_create->format('d.m.Y')]);
            echo $this->Form->hidden('channel_opening_hours.4.date_change', ['value' => $channel->channel_opening_hours[4]->date_change->format('d.m.Y')]);

output without format:

<input type="hidden" name="channel_opening_hours[1][date_change]" value="13:">

output with format

<input type="hidden" name="channel_opening_hours[2][date_create]" value="15.03.2017">

after patching getting following errors:

'[errors]' => [
				'date_create' => [
					'dateTime' => 'The provided value is invalid'
				],
				'date_change' => [
					'dateTime' => 'The provided value is invalid'
				]
			],
			'[invalid]' => [
				'date_create' => '15.03.2017',
				'date_change' => '15.03.2017'
			],

I fixed those errors. The problem was the format defined in bootstrap.php.

1 Like

Please let me know the solution. I am struggling with the same.

hello sudipday!

sorry my delayed answer. Here is the solution which is working for me:

Time::setDefaultLocale('de-DE'); // For any mutable DateTime
FrozenTime::setDefaultLocale('de-DE'); // For any immutable DateTime
Date::setDefaultLocale('de-DE');
FrozenDate::setDefaultLocale('de-DE');

Date::setToStringFormat('d.m.Y');
FrozenDate::setToStringFormat('dd.MM.Y');
FrozenTime::setToStringFormat('HH:mm');
Time::setToStringFormat('H:i');

Type::build('datetime')->useLocaleParser();
Type::build('date')->useLocaleParser();
Type::build('time')->useLocaleParser();


\Cake\I18n\I18n::locale('de_DE');

Type::build('time')->useImmutable();
Type::build('date')->useImmutable()->useLocaleParser();
Type::build('datetime')->useImmutable()->useLocaleParser();
Type::build('timestamp')->useImmutable();