Answer the question
In order to leave comments, you need to log in
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',
]
public function rules()
{
return [
[['timestamp_start'], 'date', 'format' => 'php:Y-m-d H:i:s'],
];
}
<?=
$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,
]
]);
?>
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question