yii2 gridview filter date

人走茶凉 提交于 2019-12-11 06:08:34

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!