yii2 gridview过滤日期 [英] yii2 gridview filter date

查看:106
本文介绍了yii2 gridview过滤日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在gridview中有一个日期。我想以 29.11.2018 格式输入。但是在我的表中,日期的格式为 2018-11-29
因此它不会过滤。
我的index.php:

I have in my gridview a date. I want to type it in the format "29.11.2018". But in my table, the date is in the format "2018-11-29". So it doesnt filter. my index.php:

[
'attribute'=>'enddate',
'format' => 'date',
'label' => 'Ablaufdatum'
],

我的搜索模型:

 $query->andFilterWhere(['like', 'user', $this->user])
      ->andFilterWhere(['like', 'enddate', $this->enddate]);


推荐答案

您可以使用 mysql: DATE_FORMAT() php:date()以确保它总是以相同的格式进行比较,并且您可以使用任何 = > < > ; = < = 运算符,而不是像这样的

You can use mysql:DATE_FORMAT() AND php:date() to make sure it always compares in the same format and you can use any of the =,>,<,>=,<= operators rather than like.

更改以下代码

$query->andFilterWhere(['like', 'user', $this->user])
      ->andFilterWhere(['like', 'enddate', $this->enddate]);

$query->andFilterWhere(['like', 'user', $this->user])
    ->andFilterWhere(
        [
            '=',
            new \yii\db\Expression('DATE_FORMAT(enddate, "%d.%m.%Y")'),
            date('d.m.Y', strtotime($this->enddate)),
        ]
    );

这篇关于yii2 gridview过滤日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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