Yii2 datecontrol datetime conversion not working properly

I´ve been learning Yii2 framework the past 2 weeks. I´m doing some tests to update and retrieve datetime fields.

I create a Mysql Table “order” with ‘created_date’ field as “DATETIME”.

I configured the \kartik\datecontrol\Module this way:


 'datecontrol' => [ 

                    'class' => '\kartik\datecontrol\Module',


                    // format settings for displaying each date attribute (ICU format example)

                    'displaySettings' => [ 

                            Module::FORMAT_DATE => 'dd/MM/yyyy',

                            Module::FORMAT_TIME => 'HH:mm:ss a',

                            Module::FORMAT_DATETIME => 'dd/MM/yyyy HH:mm:ss a' 

                    ],


                    // format settings for saving each date attribute (PHP format example)

                    'saveSettings' => [ 

                            Module::FORMAT_DATE => 'php:Y-m-d',

                            Module::FORMAT_TIME => 'php:H:i:s',

                            Module::FORMAT_DATETIME => 'php:Y-m-d H:i:s' 

                    ],


                    // set your display timezone

                    'displayTimezone' => 'America/Sao_Paulo',


                    // set your timezone for date saved to db

                    'saveTimezone' => 'UTC',


                    // aut;omatically use kartik\widgets for each of the above formats

                    'autoWidget' => true,


                    // default settings for each widget from kartik\widgets used when autoWidget is true

                    'autoWidgetSettings' => [ 

                            Module::FORMAT_DATE => [ 

                                    'type' => 2,

                                    'pluginOptions' => [ 

                                            'autoclose' => true 

                                    ] 

                            ], // example

                            Module::FORMAT_DATETIME => [ ], // setup if needed

                            Module::FORMAT_TIME => [ ] 

                    ], // setup if needed*/


                    // custom widget settings that will be used to render the date input instead of kartik\widgets,

                       // this will be used when autoWidget is set to false at module or widget level.

                    'widgetSettings' => [ 

                            Module::FORMAT_DATE => [ 

                                    'class' => 'yii\jui\DatePicker', // example

                                    'options' => [ 

                                            'dateFormat' => 'php:d-M-Y',

                                            'options' => [ 

                                                    'class' => 'form-control' 

                                            ] 

                                    ] 

                            ] 

                    ] 

            ],

My update view:


<?= $form->field($model, 'created_date')->widget(DateControl::classname(), [

    'type'=>DateControl::FORMAT_DATETIME,

    'ajaxConversion'=>true,

    /*'options' => [

        'pluginOptions' => [

            'autoclose' => true

        ]

    ],*/

    'autoWidget' => true,

    'displayTimezone'=>'America/Sao_Paulo',

    'saveTimezone'=>'UTC'

]);

?>

My test, putting the current Sao Paulo timezone date and time

(SPAM checker doesnt allow me to put the image and not even the correct url!)

After update the record, I check the DB. The expected result (on my understanding) would be:

2015-06-04 17:00:00 (The UTC date time for given input)

But it´s not working. It´s saving as:

2015-06-05 06:00:00

And on view it´s being displayed as:

(SPAM checker doesnt allow me to put the image and not even the correct url!)

Any help?

Thanks

Anyone?

Thanks

Have you tried to debug it by skipping the timezones (displayTz and/or saveTz) configuration and see what is displayed/saved in db? Maybe your db/system is forcing different timezone?

Hello Bizley

14/06/2015 22:30pm (local) -> 2015-06-15 13:30:00 (db)

Without the configuration

//‘displayTimezone’ => ‘America/Sao_Paulo’,

//‘saveTimezone’ => ‘UTC’,

14/06/2015 22:30pm (local) -> 2015-06-15 10:30:53 (db)

Only with:

//‘displayTimezone’ => ‘America/Sao_Paulo’,

‘saveTimezone’ => ‘UTC’,

14/06/2015 22:30pm (local) -> 2015-06-15 15:30:53 (db)

With:

‘displayTimezone’ => ‘America/Sao_Paulo’,

‘saveTimezone’ => ‘UTC’,

'on afterOpen' =&gt; function(&#036;event) {


  // &#036;event-&gt;sender refers to the DB connection


   &#036;event-&gt;sender-&gt;createCommand(&quot;SET time_zone = '+00:00'&quot;)-&gt;execute();


}

14/06/2015 22:30pm (local) -> 2015-06-15 13:30:53 (db)

That´s weird, the expected result should be 2015-06-15 01:30:53 on db…

I think I solved the problem.

Module::FORMAT_DATETIME => ‘dd/MM/yyyy HH:mm:ss a’ is not correct.

The correct is:

Module::FORMAT_DATETIME => ‘dd/MM/yyyy hh:mm:ss a’

or

Module::FORMAT_DATETIME => ‘dd/MM/yyyy HH:mm:ss’

I see this is 24/12-hours confusion.

Technically all the formats you have given are correct - the first one has got am/pm marker which is overkill since HH is hour in 24-hour format.

Here is the list of ICU symbols http://userguide.icu-project.org/formatparse/datetime