Welcome Guest, Not a member yet? Register   Sign In
Best practice for date in entity
#1

hello

in a form i have a date in format 'dd/mm/yyyy'

i use entity and i want save the date in sql with model

i do in my model
Code:
$myEntity = new TheEntity($data);
        if ($this->save($myEntity))

this send me an error

DateTime::__construct(): Failed to parse time string (19/03/2016) at position 0 (1): Unexpected character


i have create a setter in my entity
Code:
protected $dates = ['date_naissance'];
   
    public function setDateNaissance($date)
    {
        if (empty($date)) {
            $this->attributes['date_naissance'] = null;
            return $this;
        }else{
            $this->attributes['date_naissance'] = date('Y-m-d', strtotime(str_replace('/', '-', $date)));
            return $this;
        }
    }
but the setter isn't call before the error
Reply
#2

The date is saved as an integer your trying to pass illegal characters into it remove all / -
20201012
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

in CI4 they have the Time class, try to use this

use CodeIgniter\I18n\Time;

then you can format the form date to mysql datetime before save it to DB
Reply




Theme © iAndrew 2016 - Forum software by © MyBB