Skip to content

eLFuvo/yii2-date-compare

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Latest Stable Version Build Total Downloads License

Requirements

  • PHP >=7.4

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist elfuvo/yii2-date-compare "~0.1.0"

or add

"elfuvo/yii2-date-compare": "~0.1.0"

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

Configure

In application configuration file set defaultTimeZone (DB time zone) and timeZone (application time zone) for formatter

    [
        'components' => [
            'formatter' => [
                'class' => \yii\i18n\Formatter::class,
                'defaultTimeZone' => 'UTC', // database timezone
                'timeZone' => 'Europe/Moscow', // application time zone
            ],
        ],
    ];

Usage

Add DateCompareActiveQueryTrait to your ActiveQuery class. Then use where functions on selection.

Custom::find()->andCompareDate('>', Custom::tableName().'.[[createdAt]]', $model->dateFrom);

Custom::find()->orCompareTime('<=', Custom::tableName().'.[[createdAt]]', $model->dateTimeFrom);

Custom::find()->orWhere([
    'AND',
    ['>=', Custom::tableName().'.[[createdAt]]', DateConvertHelper::toDefaultTime($model->dateTimeFrom)],
    ['<=', Custom::tableName().'.[[createdAt]]', DateConvertHelper::toDefaultTime($model->dateTimeTo)],
]);

In filter view you must use yii formatter:

$form->field($model, 'dateFrom')->textInput(['value' => Yii::$app->formatter->asDate($model->dateFrom, 'dd.MM.yyyy')]);

You can use DateValueWidget for date formatting:

$form->field($model, 'dateFrom')->widget([
    'class'=> DateValueWidget::class,
    'format' =>'dd.MM.yyyy HH:mm'
]);

You can define default date formatting for DateValueWidget in container definitions:

[
    'containers' => [
        'definitions' =>[
            \elfuvo\dateCompare\widgets\DateValueWidget::class =>[
                'class' => \elfuvo\dateCompare\widgets\DateValueWidget::class,
                'format' => 'dd/MM/yyyy',
            ],
        ],  
    ]
];

About

Compare dates in database with time zone set

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages