How data provider can return results for last 24h

I need to return results from database table only where updated_at 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.

Thanks in advance

Hi,

Basically you have to do:

  • calculate how many seconds 24 hours have



$last_24_hours = 60 * 60 * 24; // 3637440 seconds 



  • get current timestamp



$current_time=time();



  • calculate 24 hours old timestamp



$yesterday_stamp = $current_time - $last_24_hours;



And modify your query to receive only data that is younger than your "yesterday" timestamp:





$query = SearchStats::find(); 

// select where updated_at is bigger or equals $yesterday_timestamp

$query->where('updated_at >= '.$yesterday_stamp);


$dataProvider = new ActiveDataProvider([

    'query' => $query,

    'pagination' => [

        'pageSize' => 10,

    ],

]);



The code above is not tested but I think it should work like this.

Best Regards