V
V
VicTHOR2021-02-11 11:48:42
Yii
VicTHOR, 2021-02-11 11:48:42

How to format dates by default with DatePicker plugin?

<?= DatePicker::widget([
        'model' => $model,
        'attribute' => 'date_start',
        'attribute2' => 'date_end',
        'options' => [
            'placeholder' => 'Дата начала',
            'autocomplete' => 'off'
        ],
        'options2' => [
            'placeholder' => 'Дата окончания',
            'autocomplete' => 'off',
        ],
        'type' => DatePicker::TYPE_RANGE,
        'form' => $form,
        'separator' => '→',
        'pluginOptions' => [
            'format' => 'dd.MM.yyyy',
            'autoclose' => true,
            'todayHighlight' => true,
        ],
    ]) ?>

The correct date format is substituted after selection, but it is stored in the database in a different format yyyy-MM-dd.

Everything works correctly, but if you create a model with dates and then open the form for editing, the DatePicker fields are substituted with model values ​​in the format in which they are stored in the database.

How to fix this format?
I tried in the model to do
public function getDateStart()
    {
        return Yii::$app->formatter->asDate($this->date_start, 'php:d.m.Y');
    }
Does not help.
I tried to put value in the plugin, it ignores it (probably due to the fact that the model is specified).

The problem is that when editing, the format specified in the plugin is not recognized and the year 1917 is shown.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question