We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

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.



85.5k

https://docs.phalcon.io/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
    }
}


93.7k
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...