How to Compare Dates from a Database in Yii2

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

This is the code for today's programs whose close_date greater than today date . I get an error message:

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

+5
source share
2 answers

If you want to write the where clause as an array, the code should look like this:

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

Check the official documentation for more details:

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

+8
source

Or like this code:

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

Source: https://habr.com/ru/post/1213203/


All Articles