如何在Yii 2 ActiveRecord中比较日期和时间?

时间:2019-02-22 11:09:49

标签: php activerecord yii yii2 sql-timestamp

我完全感到困惑,如何在Yii ActiveRecord中比较日期和时间?

我的模型中有此方法:

public static someMethod(){
    self::updateAll(['status' => SomeModel::STATUS_ACTIVE], [
        'status' => SomeModel::STATUS_ONLINE, 
        'last_seen_timestamp_no_timezone + 15 minutes < now()' 
    ]);
}

如何在我的updateAll()调用中添加条件:last_seen_timestamp_no_timezone + 15 minutes < now()

1 个答案:

答案 0 :(得分:2)

您可以使用php:

public static someMethod(){
   self::updateAll(['status' => SomeModel::STATUS_ACTIVE], [
       'AND',
       ['status' => SomeModel::STATUS_ONLINE], 
       ['<', 'last_seen_timestamp_no_timezone', date('Y-m-d H:i:s', strtotime('-15 minutes'))]
   ]);
}