maddoger / yii2-datetime-behavior
Yii2 DateTime Behavior
Installs: 314
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
Type:yii2-extension
pkg:composer/maddoger/yii2-datetime-behavior
Requires
- yiisoft/yii2: ~2.0.13
This package is not auto-updated.
Last update: 2025-10-26 01:53:27 UTC
README
Installation
The preferred way to install this extension is through composer.
Either run
php composer.phar require --prefer-dist maddoger/yii2-datetime-behavior "*"
or add
"maddoger/yii2-datetime-behavior": "*"
to the require section of your composer.json file.
Usage
For datetime format and timezone conversation you can use DateTimeBehavior.
In model behaviors for timestamp field:
[
'class' => DateTimeBehavior::className(),
'attributes' => ['published_at'],
'originalFormat' => 'U', //original format
'originalTimeZone' => 'UTC', //original timezone
'timeZone' => 'Europe/London', //local timezone
'format' => 'datetime', //local format, Formatter format
]
Now you can use published_at as original attribute and published_at_local as user read-write attribute.
For date fields:
[
'class' => DateTimeBehavior::className(),
'attributes' => ['birth_date'],
'originalFormat' => 'Y-m-d',
'format' => 'date',
'timeZone' => 'UTC',
]