Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii2 query records only if the date time field is greater than today

Tags:

yii2

I have a mysql table with one of the fields as datetime in the datetime format. I want to change the following query to one that will check the datetime of each record and only return those records that are greater than the current time and date.

$dataProvider = new ActiveDataProvider([
        'query' => NflLines::find(),
    ]);
like image 550
Shawn Sonnier Avatar asked Dec 20 '22 00:12

Shawn Sonnier


1 Answers

Assuming your column with datetime is named datetime, you can do it like this:

use yii\data\ActiveDataProvider;
use yii\db\Expression;

...

$query = NflLines::find()->where(['>', 'datetime', new Expression('NOW()')]);
$dataProvider = new ActiveDataProvider(['query' => $query]);

Read more about ways of specifying where condition here and wrapping with expression here in official docs.

like image 159
arogachev Avatar answered May 12 '23 05:05

arogachev