V
V
Vladimir Soldatov2018-02-06 13:56:13
MySQL
Vladimir Soldatov, 2018-02-06 13:56:13

How to save date and time formatted via formatter in Yii2 in activeRecord?

mysql field timestamp_start type TIMESTAMP default CURRENT_TIMESTAMP
Yii2 formatter config

'formatter' => [
            'locale' => 'ru-RU',
            'defaultTimeZone' => 'Europe/Moscow',
            'timeZone' => 'Asia/Yekaterinburg',
            'dateFormat' => 'dd.MM.yyyy',
            'timeFormat' => 'HH:mm:ss',
            'datetimeFormat' => 'dd.MM.yyyy HH:mm:ss',
]

Validation in the model by storage format
public function rules()
{
     return [
            [['timestamp_start'], 'date', 'format' => 'php:Y-m-d H:i:s'],
     ];
}

the form
<?=
$form->field($model, 'timestamp_start')->widget(kartik\datetime\DateTimePicker::classname(), [
        'options' => [
                'value' => (!$model->timestamp_start) ? Yii::$app->formatter->asDatetime(date('Y-m-d H:i:s')) : Yii::$app->formatter->asDatetime($model->timestamp_start),
         ],
        'pluginOptions' => [
                'startDate' => Yii::$app->formatter->asDatetime(date('Y-m-d H:i:s')),
                'format' => Yii::$app->params['jsFormatter']['datetimeFormat'],
                 'autoclose' => true,
         ]
]);
?>

How to correctly save the formatted date from the form in the native time zone ('Europe/Moscow').
Digging towards behaviors there are events:
ActiveRecord::EVENT_BEFORE_INSERT
ActiveRecord::EVENT_BEFORE_UPDATE
ActiveRecord::EVENT_BEFORE_VALIDATE
Wrote a handler for reformatting and changing the time zone via DateTime
Formatting is successful, but the time zone does not change back.
Tried through beforeValidate () - the same
result As a result, each save + 2 hours.
MySQL 5.7
PHP 7.1

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
B
Boris Korobkov, 2018-02-06
@TPbIHTPABA

The easiest way is to store both in the database and in a variable in the format "Ymd H:i:s".
And display and enter in the widget in any desired format. As you type, the widget will automatically change the format.
In config add type

'modules' => [
                        'datecontrol' => [
                                'class' => 'kartik\datecontrol\Module',
                                // format settings for displaying each date attribute (ICU format example)
                                'displaySettings' => [
                                        'date' => 'dd.MM.yyyy',
                                        'time' => 'HH:mm',
                                        'datetime' => 'dd.MM.yyyy HH:mm',
                                ],
                                // format settings for saving each date attribute (PHP format example)
                                'saveSettings' => [
                                        'date' => 'php:Y-m-d', // saves as unix timestamp
                                        'time' => 'php:H:i:s',
                                        'datetime' => 'php:Y-m-d H:i:s',
                                ],
                        ],
                ],

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question