How to compare Dates from database in Yii2

2020-06-18 19:28发布

$time = new \DateTime('now');
$today = $time->format('Y-m-d');
$programs=Programs::find()->where(['close_date' >= $today])->all();

This is code for today's programs whose close_date is greater than today's date. I am getting error:

"Invalid Parameter -yii\base\InvalidParamException Operator '1' requires two operands".

标签: yii2
2条回答
Lonely孤独者°
2楼-- · 2020-06-18 19:56

If you want to write where condition as array the code should be like this:

$programs = Programs::find()->where(['>=', 'close_date', $today])->all();

Check official documentation for more details:

Additionally you can specify arbitrary operators as follows: A condition of ['>=', 'id', 10] will result in the following SQL expression: id >= 10.

查看更多
萌系小妹纸
3楼-- · 2020-06-18 20:07

Or like this code:

$programs = Programs::find()->where('close_date >= :close_date', [':close_date' => $today])->all();
查看更多
登录 后发表回答