问题
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'
],
my searchmodel:
$query->andFilterWhere(['like', 'user', $this->user])
->andFilterWhere(['like', 'enddate', $this->enddate]);
回答1:
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.
Change the below code
$query->andFilterWhere(['like', 'user', $this->user])
->andFilterWhere(['like', 'enddate', $this->enddate]);
to
$query->andFilterWhere(['like', 'user', $this->user])
->andFilterWhere(
[
'=',
new \yii\db\Expression('DATE_FORMAT(enddate, "%d.%m.%Y")'),
date('d.m.Y', strtotime($this->enddate)),
]
);
回答2:
You can try something like this before filter:
date( 'Y-m-d', strtotime( $this->enddate ) );
来源:https://stackoverflow.com/questions/53541201/yii2-gridview-filter-date