Isengo
Isengo

Reputation: 2083

CakePHP 3.x and datetimepicker

I am using https://github.com/xdan/datetimepicker to select a date in my add function. Works like a charm. Now when I want to edit it - it inputs a weird date.

Custom Changes: bootstrap.php

date_default_timezone_set('Europe/Berlin');

edit.ctp

echo $this->Form->input('datetime', ['label' => __('Datum'), 'type'=>'text','id' => 'datetimepicker','selected' => '0000-00-00 00:00:00']);

datetimepicker

$('#datetimepicker').datetimepicker({
format:'Y-m-d H:i',
lang: 'de'
});

Output in my view:

<input type="text" name="datetime" id="datetimepicker" value="16.10.19 15:00">

debug Output from my edit view

'datetime' => object(Cake\I18n\FrozenTime) {

        'time' => '2019-10-16T15:00:00+02:00',
        'timezone' => 'Europe/Berlin',
        'fixedNowTime' => false

    }

Change the date_default_timezone_set already to UTC. But this doesn´t help. I wonder where my app gets the idea from putting this in my output. When I select it with my dtp again and choose a date it also works - but it takes a random date like 2019-10-10 and you have to go all the way back, which is quite inconvenient :/

Any ideas?

EDIT

I added this in my edit() function before I patched the Entity and it workd.

$adress['datetime'] = $adress['datetime']->i18nFormat('yyyy-MM-dd HH:mm');

Upvotes: 3

Views: 3712

Answers (2)

Isengo
Isengo

Reputation: 2083

I added this in my edit() function before I patched the Entity and it works now. Hope this will help others. If somebody knows a better way, please share! I think this is just a workaround.

$adress['datetime'] = $adress['datetime']->i18nFormat('yyyy-MM-dd HH:mm');

Upvotes: 0

Sumon Sarker
Sumon Sarker

Reputation: 2795

You just set the server timezone using date_default_timezone_set('Europe/Berlin'); But JavaScript doesn't care about server timezone, if you doesn't set. JavaScript always try to use your PC time. So, You need to set datetimepicker timezone using JavaScript, if datetimepicker have the timezone option.

OR

Try to change your edit.ctp Code like below -

echo $this->Form->input(
 'datetime',
 [
  'label' => __('Datum'),
  'type'=>'text',
  'id' => 'datetimepicker',
  'default' => date('Y-m-d H:i') #Set time for today
 ]
);

Upvotes: 2

Related Questions