PHP code example of bupy7 / yii2-datetime-converter

1. Go to this page and download the library: Download bupy7/yii2-datetime-converter library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

bupy7 / yii2-datetime-converter example snippets


'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);

$datetime = new DateTime('now');
echo Yii::$app->dtConverter->toDisplayDateTime($datetime);

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'],
            ],
        ],
    ];
}

php composer.phar