yii2 gridview filter date

yii2__

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]);
Muhammad Omer Aslam

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related