PHPackages                             mixapaco/yii2-date-range - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. mixapaco/yii2-date-range

ActiveYii2-extension[Utility &amp; Helpers](/categories/utility)

mixapaco/yii2-date-range
========================

An advanced Yii 2 date range picker input for based on bootstrap-daterangepicker plugin.

02JavaScript

Since Oct 30Pushed 1y agoCompare

[ Source](https://github.com/mixapaco/yii2-date-range)[ Packagist](https://packagist.org/packages/mixapaco/yii2-date-range)[ RSS](/packages/mixapaco-yii2-date-range/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)DependenciesVersions (1)Used By (0)

 [ ![Krajee Logo](https://camo.githubusercontent.com/4addfbb3869c3fc7d79befea4f06d9cf3655a686fb503df0da1d725859cfdef9/687474703a2f2f6b617274696b2d762e6769746875622e696f2f626f6f7473747261702d66696c65696e7075742d73616d706c65732f73616d706c65732f6b72616a65652d6c6f676f2d622e706e67) ](http://demos.krajee.com "Krajee Demos")
 yii2-date-range ---

 [ ![Donate](https://camo.githubusercontent.com/4af77d425ca202e55ab3d711b438e238ded194735bc345a019ac060e03e26227/687474703a2f2f6b617274696b2d762e6769746875622e696f2f626f6f7473747261702d66696c65696e7075742d73616d706c65732f73616d706c65732f646f6e6174652e706e67) ](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=DTP3NZQ6G2AYU "Donate via Paypal")
===================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================

[](#------------------------yii2-date-range--------------------)

[![Stable Version](https://camo.githubusercontent.com/e4293c33d0be3a63e52db2f7698e0985cff25c201e57187980941be862da948b/68747470733a2f2f706f7365722e707567782e6f72672f6b617274696b2d762f796969322d646174652d72616e67652f762f737461626c65)](https://packagist.org/packages/kartik-v/yii2-date-range)[![Untable Version](https://camo.githubusercontent.com/78e9819e8255554b79542c91ee6d4430ecb154185279ccf7f3224b4aaed1841f/68747470733a2f2f706f7365722e707567782e6f72672f6b617274696b2d762f796969322d646174652d72616e67652f762f756e737461626c65)](https://packagist.org/packages/kartik-v/yii2-date-range)[![License](https://camo.githubusercontent.com/a708792de4d88ab7d9bd5838e7960fca2a082fa9459427d1a868e09ae80063b7/68747470733a2f2f706f7365722e707567782e6f72672f6b617274696b2d762f796969322d646174652d72616e67652f6c6963656e7365)](https://packagist.org/packages/kartik-v/yii2-date-range)[![Total Downloads](https://camo.githubusercontent.com/845f63f123080e71842e311afcfe2a21cabcb02c6ea6456d49c4798ba0b419dc/68747470733a2f2f706f7365722e707567782e6f72672f6b617274696b2d762f796969322d646174652d72616e67652f646f776e6c6f616473)](https://packagist.org/packages/kartik-v/yii2-date-range)[![Monthly Downloads](https://camo.githubusercontent.com/3ba2234b35a0eee43196bf656331a111ae090f8a5c5fb5c1255623e002bdc356/68747470733a2f2f706f7365722e707567782e6f72672f6b617274696b2d762f796969322d646174652d72616e67652f642f6d6f6e74686c79)](https://packagist.org/packages/kartik-v/yii2-date-range)[![Daily Downloads](https://camo.githubusercontent.com/73ba325349c32bb1f11285a205d38cf5b23c9462aa914c0517881d6762649ea1/68747470733a2f2f706f7365722e707567782e6f72672f6b617274696b2d762f796969322d646174652d72616e67652f642f6461696c79)](https://packagist.org/packages/kartik-v/yii2-date-range)

An advanced date range picker input for Yii Framework 2 based on [dangrossman/bootstrap-daterangepicker plugin](https://github.com/dangrossman/bootstrap-daterangepicker). The date range picker widget is styled for Bootstrap 3.x and 4.x versions and creates a dropdown menu from which a user can select a range of dates. If the plugin is invoked with no options, it will present two calendars to choose a start and end date from. Optionally, you can provide a list of date ranges the user can select from instead of choosing dates from the calendars. If attached to a text input, the selected dates will be inserted into the text box. Otherwise, you can provide a custom callback function to receive the selection.

Additional enhancements added for this widget (by Krajee):

- allows ability to work with Bootstrap input group addons and set the picker position to point at the input-group-addon icon.
- enhanced translation features using yii i18n message files.
- automatically convert format from PHP Date/time format to Moment.js Date/time format.
- automatically trigger change of base field to enforce Yii ActiveField validation
- ability to set the widget to display a preset dropdown of date options within a container (and hidden input).
- style the container options as per your need using templates
- automatically disable date-range based on disabled/readonly options.

### Demo

[](#demo)

You can see detailed [documentation](http://demos.krajee.com/date-range) on usage of the extension.

### Release Changes

[](#release-changes)

Refer the [CHANGE LOG](https://github.com/kartik-v/yii2-date-range/blob/master/CHANGE.md) for details of various releases.

Installation
------------

[](#installation)

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

> Note: Check the [composer.json](https://github.com/kartik-v/yii2-date-range/blob/master/composer.json) for this extension's requirements and dependencies. Read this [web tip /wiki](http://webtips.krajee.com/setting-composer-minimum-stability-application/) on setting the `minimum-stability` settings for your application's composer.json.

Either run

```
$ php composer.phar require kartik-v/yii2-date-range "dev-master"

```

or add

```
"kartik-v/yii2-date-range": "dev-master"

```

to the `require` section of your `composer.json` file.

Usage
-----

[](#usage)

### DateRangePicker

[](#daterangepicker)

```
use kartik\daterange\DateRangePicker;
echo DateRangePicker::widget([
    'model'=>$model,
    'attribute'=>'datetime_range',
    'convertFormat'=>true,
    'pluginOptions'=>[
        'timePicker'=>true,
        'timePickerIncrement'=>30,
        'locale'=>[
            'format'=>'Y-m-d h:i A'
        ]
    ]
]);
```

or using seperate min/max attributes on model

```
use kartik\daterange\DateRangePicker;
echo DateRangePicker::widget([
    'model'=>$model,
    'attribute'=>'datetime_range',
    'convertFormat'=>true,
    'startAttribute'=>'datetime_min',
    'endAttribute'=>'datetime_max',
    'pluginOptions'=>[
        'timePicker'=>true,
        'timePickerIncrement'=>30,
        'locale'=>[
            'format'=>'Y-m-d h:i A'
        ]
    ]
]);
```

### DateRangeBehavior

[](#daterangebehavior)

```
use kartik\daterange\DateRangeBehavior;

class UserSearch extends User
{
    public $createTimeRange;
    public $createTimeStart;
    public $createTimeEnd;

    public function behaviors()
    {
        return [
            [
                'class' => DateRangeBehavior::className(),
                'attribute' => 'createTimeRange',
                'dateStartAttribute' => 'createTimeStart',
                'dateEndAttribute' => 'createTimeEnd',
            ]
        ];
    }

    public function rules()
    {
        return [
            // ...
            [['createTimeRange'], 'match', 'pattern' => '/^.+\s\-\s.+$/'],
        ];
    }

    public function search($params)
    {
        $query = User::find();
        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);
        $this->load($params);
        if (!$this->validate()) {
            $query->where('0=1');
            return $dataProvider;
        }

        $query->andFilterWhere(['>=', 'createdAt', $this->createTimeStart])
              ->andFilterWhere(['
