DateTime not saving correctly in cakephp 3.x with bootstrap datetimepicker

I am using cakephp 3.x, I have one form in which one field has a date. in the backend I use mysql.

my field structure is in mysql dobtype date.

now in cakephp 3.x I used the below syntax to create input.

echo $this->Form->input('dob', array(
    'label' => (__('Date of Birth')),
    'type' => 'text',
    'required' => false,
    'class' => 'form-control date'
));

and I used bootstrap datetimepicker for example

$('.date').datetimepicker({
    format: 'YYYY-MM-DD'
});

now when i submit the at form and i print_rrequest data at this time i got this field like this

[
....
'dob' => '2016-02-11',
....
]

but when I save the record and look in the database then it shows me a random date like 2036-10-25

can someone help me?

+4
source share
2

,

//File : src/Model/Table/PatientsTable.php
namespace App\Model\Table;

use Cake\ORM\Table;
use Cake\Event\Event;
use ArrayObject;
use Cake\I18n\Time;

class PatientsTable extends Table
{
    ...
    ...
    public function beforeMarshal(Event $event, ArrayObject $data, ArrayObject $options)
    {
        if (isset($data['dob'])) {
            $data['dob'] = Time::parseDate($data['dob'], 'Y-M-d');
        }
    }
}
+1

dob , .

use Cake\I18n\Time;

$this->request->data['dob']= Time::parseDate($this->request->data['dob'],'Y-M-d');
0

Source: https://habr.com/ru/post/1628272/


All Articles