How to compare Dates from database in Yii2

2020-06-18 20:05发布

问题:

$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".

回答1:

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.



回答2:

Or like this code:

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


标签: yii2