如何在 Yii2 中比较数据库中的日期 [英] How to compare Dates from database in Yii2

查看:29
本文介绍了如何在 Yii2 中比较数据库中的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这是用于close_date 大于今天的日期 的今天程序的代码.我收到错误:

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

"无效参数 -yii\base\InvalidParamException Operator '1'需要两个操作数".

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

推荐答案

如果你想把 where 条件写成数组,代码应该是这样的:

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

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

查看官方文档了解更多详情:

此外,您可以指定任意运算符,如下所示:A['>=', 'id', 10] 的条件将导致以下 SQL表达式:id >= 10.

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

这篇关于如何在 Yii2 中比较数据库中的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆