Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Timestamp in model?

Hello,

I have a timestamp field in MySQL table, generated the model using the dev-tools. Assigning an unix timestamp to the column and saving, results in 0000-00-00 00:00:00. I could not find a way to force Phalcon to do something like FROM_UNIXTIME(1500000000)

How are you saving saving unix timestamps into a timestamp MySQL field?

Thanks.



67.7k

https://docs.phalconphp.com/en/latest/reference/models.html if you use beforeSave event ?


function beforeSave(){
    if ($this->myTimeStamp_field != 0 ) { //or whatever
        $dt = new \DateTime();
        $dt->setTimestamp($this->myTimeStamp_field);
        $this->myTimeStamp_field = $dt->format("d-m-Y h:i"); //you have to hceck the format i dont know it by heart
    }
}


69.5k
Accepted
answer

You can use MySQL built in functions in models like this:

public function beforeValidation()
{
    $this->date_field = new \Phalcon\Db\RawValue('FROM_UNIXTIME(1500000000)');
}

Exactly what I've been looking for, how can I miss it...