Sachy
(Sachin Mandalia)
October 10, 2014, 7:15pm
1
I printed out the actually model post attributes as below
Array (
[subject] => I would like to Reserve
[is_read] => 0
[ReservationDate] => 22/10/2014
[body] => sdfsdf [id] => [sender_id] => [receiver_id] => [Vessel_Vesselid] => [deleted_by] => [created_at] => )
$model->save() calls successfully which saves all the fields in the database
but the date is not saved in database!! (see Reservationdate)
I am using MySQL and datatype is Date
5976
georaldc
(Georaldc)
October 10, 2014, 7:31pm
2
Dates in mysql are usually formatted as Y-m-d (or Y-m-d H:i:s for DateTime). You probably need to reformat your entered dates before it reaches your database. You can do this in your model, maybe inside your model’s beforeSave() method
public function beforeSave()
{
if (parent::beforeSave())
{
if ($this->ReservationDate !== null)
{
$this->ReservationDate = date('Y-m-d', strtotime($this->ReservationDate));
}
return true;
}
return false;
}
or something similar like that
Sachy
(Sachin Mandalia)
October 10, 2014, 7:34pm
3
georaldc:
Dates in mysql are usually formatted as Y-m-d (or Y-m-d H:i:s for DateTime). You probably need to reformat your entered dates before it reaches your database. You can do this in your model, maybe inside your model’s beforeSave() method
public function beforeSave()
{
if (parent::beforeSave())
{
if ($this->ReservationDate !== null)
{
$this->ReservationDate = date('Y-m-d', strtotime($this->ReservationDate));
}
return true;
}
return false;
}
or something similar like that
Ah Thanks!! I guess that should work
Sachy
(Sachin Mandalia)
October 11, 2014, 9:33am
4
Unfortunately it stores the date but wrong date as " 1970-01-01"
Found a solution for this
date(‘Y-m-d’, strtotime(str_replace("/","-",$this->ReservationDate)));