PHP code example of silentlun / yii2-daterangepicker

1. Go to this page and download the library: Download silentlun/yii2-daterangepicker library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

silentlun / yii2-daterangepicker example snippets


use silentlun\daterange\DateRangePicker;
echo DateRangePicker::widget([
    'name' => 'daterange',
    'attribute' => 'datetime_range',
    'pluginOptions' => [
        'timePicker' => true,
        'locale' => [
            'format' => 'YYYY-MM-DD'
        ]
    ]
]);

use silentlun\daterange\DateRangePicker;
echo DateRangePicker::widget([
    'model' => $model,
    'attribute' => 'datetime_range',
    'pluginOptions' => [
        'timePicker' => true,
        'locale' => [
            'format'=>'YYYY-MM-DD'
        ]
    ]
]);

use silentlun\daterange\DateRangePicker;
echo DateRangePicker::widget([
    'model'=>$model,
    'attribute'=>'datetime_range',
    'startAttribute'=>'datetime_start',
    'endAttribute'=>'datetime_end',
    'pluginOptions'=>[
        'timePicker'=>true,
        'timePickerIncrement'=>30,
        'locale'=>[
            'format'=>'YYYY-MM-DD'
        ]
    ]
]);
 
use silentlun\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(['<', 'createdAt', $this->createTimeEnd]);

        return $dataProvider;
    }
}

php composer