Use DB expression or PHP datetime as timestamp?

I have a temporary behavior (yii2) that looks like this (taken from an example user manual) ...

public function behaviors()
{
    return [
            'timestamp' => [
                    'class' => TimestampBehavior::className(),
                    'value' => new Expression('NOW()'),
            ],
         ];
}

      

But wouldn't it be better to use a PHP DateTime expression like ...

'value' => (new /DateTime('NOW'))->format('Y-m-d H:i:s')

      

It seems to me that it is better to work with one hour rather than two (in case the database server is separate and the time is not synchronized, which is unlikely). Especially if I use DateTime in my code to check for other conditions.

Which approach is better and why?

+3


source to share


1 answer


Expression

the class created the DBMS function inside the SQL query, it works no matter what date type (or format, you can force the date format in some DBMS) that you have in the column. If you change the meaning of the behavior, you must take care to match the format and type of the column, otherwise you would get an error for a while.



+1


source







All Articles