Search code examples
phpmysqltimestampyii2dataprovider

How Yii2 data provider can return results for last 24h


I need to return results from database table only where updated_at field is not older than 24h.

Inside my index action I have this code:

$dataProvider = new ActiveDataProvider([
    'query' => SearchStats::find(),
    'pagination' => [
        'pageSize' => 10,
    ],
]);

How can I modify this code to return me results that where inserted / updated in the past 24h hours ?

updated_at is integer, and I am using TimestampBehavior.


Solution

  • Change your query to:

    use yii\db\Expression;
    
    SearchStats::find()
        ->where(['>=', 'updated_at', new Expression('UNIX_TIMESTAMP(NOW() - INTERVAL 1 DAY)')])
    

    Related links: