简体   繁体   中英

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');

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
 ]
);

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');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM