bupy7 / yii2-datetime-converter
Converting date and/or time from saving format to display and converse.
Installs: 1 203
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 2
Open Issues: 0
Type:yii2-extension
Requires
- yiisoft/yii2: *
Requires (Dev)
- nesbot/carbon: *
- satooshi/php-coveralls: 1.0.1
This package is auto-updated.
Last update: 2020-09-11 05:28:52 UTC
README
Converting date/time from display/save to save/display format.
Installation
The preferred way to install this extension is through composer.
Either run
php composer.phar require --prefer-dist bupy7/yii2-datetime-converter "*"
or add
"bupy7/yii2-datetime-converter": "*"
to the require section of your composer.json
file.
Usage
Add component to your config:
'dtConverter' => [ 'class' => 'bupy7\datetime\converter\Converter', // 'saveTimeZone' => 'UTC' - by default // 'saveDate' => 'php:Y-m-d' - by default // 'saveTime' => 'php:H:i:s' - by default // 'saveDateTime' => 'php:U' - by default // add format patterns if need for your locales (by default uses `en`) 'patterns' => [ 'ru' => [ 'displayTimeZone' => 'Europe/Moscow', 'displayDate' => 'php:d.m.Y', 'displayTime' => 'php:H:i', 'displayDateTime' => 'php:d.m.Y, H:i', ], ], ],
$datetime = 2015-06-07 12:45:00; echo Yii::$app->dtConverter->toDisplayDateTime($datetime);
or
$datetime = new DateTime('now'); echo Yii::$app->dtConverter->toDisplayDateTime($datetime);
You can add behavior of your model for converting date/time before save.
use bupy7\datetime\converter\ConverterBehavior; public function behaviors() { return [ // converter date/time before save [ 'class' => ConverterBehavior::className(), 'type' => ConverterBehavior::TYPE_DATE_TIME, 'to' => ConverterBehavior::TO_SAVE, 'attributes' => [ self::EVENT_BEFORE_SAVE => ['attribute_1', 'attribute_2'], ], ], ]; }
##License
yii2-grid is released under the BSD 3-Clause License.