Yii2 View DateTime Format (d-m-Y H:i:s) But When Save/update in DB Change format to Y-m-d H:i:s

Kailas picture Kailas · Sep 24, 2015 · Viewed 15.8k times · Source

I'm Using Kartik DateTimePicker Extension

<?= $form->field($model, 'Created')->widget(DateTimePicker::classname(),[
        'model' => $model,
        'attribute' => 'Created',
        'name' => 'Created',
        'options' => ['placeholder' => 'Select Created'],
        'pluginOptions' => [
            'format' => 'dd-mm-yyyy hh:ii:ss',
            'todayHighlight' => true
        ]
    ]) 
?>

User fill the Create Date the format is

d-m-Y H:i:s (like 24-09-2015 11:21:10)

But when record save to database then Create Date Format change to

Y-m-d H:i:s (like 2015-09-24 11:21:10)

How can I change the date format on save/update of record

Answer

GAMITG picture GAMITG · Sep 24, 2015

Need to just add this code before save/update model in controller.

like,

// ICU format
$model->Created = Yii::$app->formatter->asDate($_POST['modelName']['Created'], 'yyyy-MM-dd HH:mm:ss'); // 2014-10-06 15:22:34

OR

// PHP date()-format
$model->Created = Yii::$app->formatter->asDate($_POST['modelName']['Created'], 'php:Y-m-d H:i:s'); // 2014-10-06 15:22:34

For more information refer this link