Yii2 gridview filter by date range

hello forum, I need my gridview filter by date range , I searched the web and saw kartik - v / yii2 -date- range, put it but when I try to filter did not return anything , anyone know how to do this function or at least you can tell me how to operate the kartik - v one?

Hi,

Use between param in your ModelSearch.php

In


public function search($params)                                    

{

...

$query->andFilterWhere(['between', 'date', $this->start_date, $this->end_date]);

...

return $dataProvider;

}

Alternatively, you could also try this:




<?php

public function search($params)                                    

{

...

$query->andFilterWhere(['>=', 'date', $this->start_date])

      ->andFilterWhere(['<=', 'date', $this->end_date]);

...

return $dataProvider;

}



This way, you can just provide an end date or a beginning date. You don’t have to enter both…

Hi Guys,

May I need to know how to add filter for created_date from date and to date range filter in view file using the kartik Gridview. I’ve used the following process but not get works to me.


use kartik\daterange\DateRangePicker;


...........


[

                        'label' => Yii::t('app', "Date"),

                        'attribute' => 'dealerAvailableDate',

                        'value' => 'customer_measurement.created_date',

                        'format' => 'datetime',

                        'filter' => DateRangePicker::widget([

                            'model' => $searchModel,

                            'attribute' => 'dealerAvailableDate',

                            'convertFormat' => true,

                            'pluginOptions' => [

                                'locale' => [

                                    'format' => 'Y-m-d'

                                ],

                            ],

                        ]),

                    ],


...............


 if (!is_null($this->dealerAvailableDate) && 

            strpos($this->dealerAvailableDate, ' - ') !== false ) {

            list($start_date, $end_date) = explode(' - ', $this->dealerAvailableDate);

            $query->andFilterWhere(['between', 'date(customer_measurement.created_date)', $start_date, $end_date]);

        }

1 Like

Hello,

can someone give detail example to filter date range in kartik gridview?

Thanks before.

Is your column Date or Datetime in database? Date range use separator to separate dates. You will need to explode string and use array items with between query

I did a manual for do this. You can follow in this url: https://tutoriales-yii2.blogspot.com/2017/09/tutorial-de-implementacion-de.html

2 Likes