How to compare Dates from database in Yii2

China☆狼群 提交于 2019-12-21 05:04:32

问题


$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();


来源:https://stackoverflow.com/questions/28470160/how-to-compare-dates-from-database-in-yii2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!