# Struggling with date / time formats

**URL:** <https://discourse.cakephp.org/t/struggling-with-date-time-formats/2246>\
**Category:** Need Help\
**Created:** [March 17, 2017, 8:39am UTC](https://discourse.cakephp.org/t/struggling-with-date-time-formats/2246 "2017-03-17T08:39:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![andrekat](https://avatars.discourse-cdn.com/v4/letter/a/eb9ed0/32.png) [@andrekat](https://discourse.cakephp.org/u/andrekat)\
**Post date:** [March 17, 2017, 8:39am UTC](https://discourse.cakephp.org/t/struggling-with-date-time-formats/2246/1 "2017-03-17T08:39:55Z")

</div>

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:**  
 ![](https://cdck-file-uploads-canada1.s3.dualstack.ca-central-1.amazonaws.com/flex029/uploads/cakephp/original/1X/97b71ec71aab14840545da5de2766a49cb912fc2.PNG)

**output**  
 ![](https://cdck-file-uploads-canada1.s3.dualstack.ca-central-1.amazonaws.com/flex029/uploads/cakephp/original/1X/c01c3a3691250fafcecd39baa7ce16671a1e0bd3.PNG)

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'
			],
```

---

<div class="post-metadata">

**Author:** ![andrekat](https://avatars.discourse-cdn.com/v4/letter/a/eb9ed0/32.png) [@andrekat](https://discourse.cakephp.org/u/andrekat)\
**Post date:** [March 17, 2017, 11:33am UTC](https://discourse.cakephp.org/t/struggling-with-date-time-formats/2246/2 "2017-03-17T11:33:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sudipdey](https://avatars.discourse-cdn.com/v4/letter/s/b487fb/32.png) [@sudipdey](https://discourse.cakephp.org/u/sudipdey)\
**Post date:** [December 15, 2017, 2:59am UTC](https://discourse.cakephp.org/t/struggling-with-date-time-formats/2246/3 "2017-12-15T02:59:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![andrekat](https://avatars.discourse-cdn.com/v4/letter/a/eb9ed0/32.png) [@andrekat](https://discourse.cakephp.org/u/andrekat)\
**Post date:** [December 21, 2017, 5:59pm UTC](https://discourse.cakephp.org/t/struggling-with-date-time-formats/2246/4 "2017-12-21T17:59:31Z")

</div>

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();
```
