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