【问题标题】:How to filter date field in yii2如何过滤yii2中的日期字段
【发布时间】:2018-07-16 16:48:04
【问题描述】:

我想过滤日期列,例如 3 个月前、6 个月前、1 年前。 我在 gridview 的搜索字段中创建了一个下拉列表,如下所示。

[
    'attribute' => 'modified',
    'value'     => 'name',
    'filter'    => array("ID1" => "Before Three months",
                         "ID2" => "Before six months",
                         "ID"  => "Before Twelve months",),
],

在模型搜索中我想搜索...

if (($this->modified) == "ID1"){
    $query->andFilterWhere(['between', $this->modified, 'today', '3monthsago']);
} 

但我不明白应该用什么来代替今天 3monthsago ? 如何在查询中计算和传递这个变量??

【问题讨论】:

    标签: date yii2


    【解决方案1】:

    将此添加到您的模型搜索中

    $today = date('Y-m-d H:i:s',time());
    if (($this->modified)=="ID1"){
        $endDate = date('Y-m-d H:i:s', strtotime('-3 months'));
        $query->andFilterWhere(['between', 'contacts.modified', $today, $endDate]);
    }elseif (($this->modified)=="ID2") {
        $endDate = date('Y-m-d H:i:s', strtotime('-6 months'));
        $query->andFilterWhere(['between', 'contacts.modified', $today, $endDate]);
    }elseif (($this->modified)=="ID") {
        $endDate = date('Y-m-d H:i:s', strtotime('-12 months'));
        $query->andFilterWhere(['between', 'contacts.modified', $today, $endDate]);
    }
    

    【讨论】:

    • 完整性约束违规 – yii\db\IntegrityException SQLSTATE[23000]: 完整性约束违规: 1052 where 子句中的“已修改”列不明确 正在执行的 SQL 为:SELECT COUNT(*) FROM @987654323 @ 左连接 companies 开启 contacts.company_id = companies.id 内连接 countries 开启 contacts.country = countries.@HER (='@9876543333333332@.country ID2') AND (ID2 BETWEEN '2017-12-08 06:10:54' AND '08/06/2017')
    • 不可能出现同样的错误。您可以通过清除 yii2 缓存 @Goli 来尝试
    • 删除文件夹YOUR_PROJECT_FOLDER/runtime/cache@Goli中的所有文件
    • 查看聊天。我已经解决了这个错误。现在既不显示错误也不显示结果。应该是什么错误?
    猜你喜欢
    • 1970-01-01
    • 2012-02-12
    • 1970-01-01
    • 2018-01-07
    • 1970-01-01
    • 1970-01-01
    • 2015-09-20
    • 2021-09-30
    • 2010-12-15
    相关资源
    最近更新 更多