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)),
]
);