I
I
Iossarian2019-02-25 13:09:25
Yii
Iossarian, 2019-02-25 13:09:25

How to convert date from DatePicker in yii2?

Good afternoon. When creating a post, the date picker format must be 'dmyyyy', actually

$form->field($model, 'date')->widget(DatePicker::className(), [
        'value' => date('d.m.yyyy', strtotime('+2 days')),
        'options' => ['placeholder' => 'Select issue date ...'],
        'pluginOptions' => [
            'format' => 'd.m.yyyy',
            'todayHighlight' => true
            ]
    ])

In the database, the date storage format is datetime, and if you write the following in the rules , then the code, accordingly, will give an error. So, how to correctly prescribe the settings in order to meet the requirements of the technical task? thank you
[['date'], 'date', 'format' => 'php:d.m.yy'],

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
M
Maxim, 2019-02-26
@Iossarian

Or in beforeSave, convert the date to the required database format. I use the written behavior and connect it in the right place to the right attributes. I can send an example if needed.
I propose to store the date in Unix format. And convert to human form. It will be more versatile.
Also the date format of this plugin should be dd.mm.yyyy
If you want to put validation for this format use format=> php: dmY

E
enchikiben, 2019-02-25
@EnChikiben

in beforeSave do

M
morricone85, 2019-02-25
@morricone85

This format definitely works: 'dd.mm.yyyy'
[['date'], 'date', 'format' => 'dd.mm.yyyy']

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question