- PHP >=7.4
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.
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
],
],
];
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',
],
],
]
];