Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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');
like image 964
Isengo Avatar asked Apr 27 '26 16:04

Isengo


1 Answers

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
 ]
);
like image 75
Sumon Sarker Avatar answered Apr 30 '26 18:04

Sumon Sarker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!