【问题标题】:How to compare dates and times in Yii 2 ActiveRecord?如何在 Yii 2 ActiveRecord 中比较日期和时间?
【发布时间】:2019-02-22 11:09:49
【问题描述】:

我很困惑,如何在 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 &lt; now()

【问题讨论】:

    标签: php activerecord yii yii2 sql-timestamp


    【解决方案1】:

    你可以用 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'))]
       ]);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-20
      • 1970-01-01
      • 1970-01-01
      • 2022-01-02
      • 2018-03-20
      • 1970-01-01
      • 2021-06-06
      相关资源
      最近更新 更多