R
R
Ruslan Absalyamov2019-02-23 19:01:22
Yii
Ruslan Absalyamov, 2019-02-23 19:01:22

How to filter date for created_at column in gridview?

I tried 2 ways, this is to change the parameters received in get requests. But I realized that they are not changeable. Then I made a new variable for the columns, the minus is that when you filter, nothing is displayed.
User models

public $date_registration;
public function rules()
    {
        return [
            ....
            [['auth_date', 'date_registration'], 'safe'],
          ...
        ];
    }
public function attributeLabels()
    {
        return [
            ....
            'date_registration' => 'Дата регистрации'
        ];
    }

UserSearch
public $date_registration;
    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            ...
            [['date_registration'], 'date', 'format' => 'php:Y-m-d']
        ];
    }
$query->andFilterWhere([
...
            'created_at' => $this->date_registration ? strtotime($this->date_registration) : null,
           ...
        ]);

gridview
<?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            ...
            [
                'attribute' => 'date_registration',
                'label' => 'Дата регистрации',
                'format' => 'datetime',
                'value' => function($date) {
                    return $date->created_at;
                },
                'filter' => Html::input('date', 'UsersSearch[date_registration]'),
            ],          
...
        ],
    ]); ?>

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question