Date formatting issues

Hello all,

Posting in the correct thread this time. Sorry moderators! :huh:

I am trying to get to grips with Yii and am getting there, but I am struggling badly with date formats.

Basically, I have a MYSQL field that is a DATE type.

This is stored as YYYY-MM-DD.

I generated the model and the CRUD via Gii and have seen the date is a string input in the CRUD, and can see that the input and display of the date is almost a direct copy of the mySQL definition. ie YYYY-MM-DD

I found this great control, not allowed to post links on my first post, so if you go to demos.krajee.com/datecontrol

I have successfully installed using composer, and have managed to use in my application via the widget on my form and have managed to get the format to work directly on the widget.

But this is directly done by editing the _form.php of my CRUD.

The creator seems to imply on the link above that if I set the params of my application I can have have a default format for both viewing and saving to date fields across my whole app.

So I thought I would tackle view first and in my application (which is a copy of the advanced Yii application) I went to common/config/params.php

I then put in the following code:


<?php

use kartik\datecontrol\Module;

return [

	'adminEmail' => 'admin@example.com',

	'supportEmail' => 'support@example.com',

	'user.passwordResetTokenExpire' => 3600,

        

        'dateControlDisplay' => [

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

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

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

    ],

];

However my date format for my date field still remains like this 2015-02-01 not dd/MM/yyyy as I had hoped.

I have asked the creator for help, but he tells me I need to review his documentation and refer to the Yii documentation on setting parameters. I have done this, but I am clearly missing something. I feel like I am drowning a bit in information and am a bit lost, so any help from anyone here would be gratefully received by a noob.

I promise I have been trying to figure this out for myself, but have spent hours for what must seem like a really simple thing to the rest of you clever people. :)

Thanks,

Mark

EDIT: if anyone wants to see the source code I have, just let me know and I can get you read only access to the git library, I have set up a test rig that just has the advanced app and this functionality poorly added to it. :)

how did you call the widget? (either paste code here, or access to the git library would do).

Ok well let me give you the code here first, if that makes no sense, I can give you access to the bitbucket code.

Right now I am just trying to get the display formatting for my field to work, but can only get that working on the input widget, not when in any other view.

So, I am using the advanced application template.

I generated CRUD code for the mySQL definition.

Then I put the following code in the _form.php


<?php


use yii\helpers\Html;

use yii\widgets\ActiveForm;

use kartik\datecontrol\DateControl;


/* @var $this yii\web\View */

/* @var $model frontend\models\DateTimeTest */

/* @var $form yii\widgets\ActiveForm */

?>


<div class="date-time-test-form">


    <?php $form = ActiveForm::begin(); ?>


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

'type'=>DateControl::FORMAT_DATE,

'displayFormat' => 'dd/MM/yyyy',

'saveFormat' => 'yyyy-MM-dd'

]); ?>


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

'type'=>DateControl::FORMAT_TIME

]); ?>


    <div class="form-group">

        <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>

    </div>


    <?php ActiveForm::end(); ?>


</div>

So that get’s the widget working fine.

Now I wanted to change the display of the date format across the board so I went to common/config/params.php

And added the following, but the general display of dates is still YYYY/MM/DD


<?php

use kartik\datecontrol\Module;

return [

        'adminEmail' => 'admin@example.com',

        'supportEmail' => 'support@example.com',

        'user.passwordResetTokenExpire' => 3600,

        

        'dateControlDisplay' => [

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

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

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

    ],

];

That is all I have changed.