yii2-datetime-converter
, (*1)
Converting date/time from display/save to save/display format., (*2)
Installation
The preferred way to install this extension is through composer., (*3)
Either run, (*4)
php composer.phar require --prefer-dist bupy7/yii2-datetime-converter "*"
or add, (*5)
"bupy7/yii2-datetime-converter": "*"
to the require section of your composer.json
file., (*6)
Usage
Add component to your config:, (*7)
'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, (*8)
$datetime = new DateTime('now');
echo Yii::$app->dtConverter->toDisplayDateTime($datetime);
You can add behavior of your model for converting date/time before save., (*9)
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., (*10)