4

i am using cakephp 3.x, i have one form in which one field is of date. in backend i am using mysql.

my field structure in mysql is dob of type date.

now in cakephp 3.x i had use 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 had used bootstrap datetimepicker like,

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

now when i submit the form at and i print_r the request data at that time i got this field like this

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

but when i save the record and look in database then it show me random date like 2036-10-25

can anyone help me please?

3
  • What locale have you set for your application? Commented Feb 11, 2016 at 15:18
  • i haven't set it. i using default one. Commented Feb 12, 2016 at 5:24
  • So it's en_US then. In that case this seems to be a duplicate of stackoverflow.com/questions/33734683/…. You should be able to apply similar to date instead of datetime, respectively to \Cake\I18n\Date instead of \Cake\I18n\Time Commented Feb 12, 2016 at 11:26

2 Answers 2

1

and this is the Final General Solution,

//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');
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

You declared dob type as date but tried to save date in string format instead of date time format. Try this

use Cake\I18n\Time;

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

1 Comment

thanks, it's works. but i need global solution that can be put in entity or somewhere else.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.