elfuvo / yii2-date-compare
Compare dates with the time zone
Installs: 2
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Open Issues: 0
Type:yii2-extension
Requires
- php: >=7.4.0
- yiisoft/yii2: ~2.0.12
Requires (Dev)
- codeception/codeception: ^4.0
- codeception/module-asserts: ^1.2
- codeception/module-cli: ^1.0
This package is auto-updated.
Last update: 2024-12-16 19:17:21 UTC
README
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', ], ], ] ];