Sao Ho
Sao Ho

Reputation: 139

Yii2 Add math in where condition

In users table I have a column call "month".

I want to list all users that meet the condition: current month - user month <=2

Here my code

$time = new \DateTime('now');
$today = $time->format('m');
$users = Users::find()->where(['<=', 'month' - $today, 2])->all();

But this code is wrong. Please help me with this.

Hope this all make sense.

Thank you!

Upvotes: 0

Views: 140

Answers (1)

ScaisEdge
ScaisEdge

Reputation: 133360

In Yii2 you can use different format for buil where condition for this kind of situatio is useful use string format with param

in string format you can pass the literal string and param this way

$users = Users::find()->where('(month - :today ) <= 2' , [':today'=>$today])->all();

see this for more http://www.yiiframework.com/doc-2.0/yii-db-query.html#where()-detail

Upvotes: 0

Related Questions