You need to write your own conversion functions to and from the model to the view.
Alternatively, you can use my yii2-datecontrol extension which will automatically set the format for save and view differently, once you have configured it. You can set the extension to use any datepicker widget.
If you use the DatePicker, TimePicker, or DateTimePicker widgets from yii2-widgets, then you have an option to autoConvert the format from PHP to JS.
You may want to post extension related queries on the related extension page and/or the project site (for the benefit of others).
You can set autoclose to true for the datepicker within the DateControl extension this way:
echo DateControl::widget([
'name'=>'kartik-date',
'type'=>DateControl::FORMAT_DATE, // uses DatePicker
'displayFormat'=>'d-M-Y', // your display format (can be set globally at module level)
'saveFormat'=>'Y-m-d', // your save format (can be set globally at module level)
'options'=>[ // this will now become the widget options for DatePicker
'pluginOptions'=>['autoclose'=>true],// datepicker plugin options
'convertFormat'=>true // autoconvert PHP date to JS date format
]
]);
@Kartik: I promised you one month ago that I will write you about the formatter issue which is used by your DateControll.
As you see in this forum entries. Users are confused about different date format patterns. In the linked PDF file you can see the different format patterns between PHP, ICU (Intl), Datepicker and JUI-Datepicker. link: PDF with Format patterns
Yii2 has following problems:
yii\base\formatter.php uses pure PHP format patterns. Your dateControl expect this format also your conversion within your DatePicker class. Disadvantage of yii/base/formatter.php is that there is no localized formats. It’s fix ‘Y-m-d’ or you have to code your own pattern. This isn’t state of the art for an international application with differen ‘$app->local’ definitions.
yii\i18n\formatter.php is extended from yii\base\formatter.php but all date formating methods are overridden. This class supports ICU (IntlFormatter class) standard only! variable “dateFormat” is defined with the word ‘short’ by default. Consequence your DateControl class will fail with this component class. ICU format pattern is completely different ‘Y-m-d’ -> ‘yyyy-mm-dd’.
Your DateControl should use the option "convertFormat" by default and it should use the "dateFormat" from formatter by default if no other format is defined. BTW: DateTimePicker has a built in format conversion in Java-Script. Theoretically this script could be used in DatePicker class.
Consquence:
I have refactored the whole formatter class which is able to work with PHP and ICU standard (internal conversion). Standard communication pattern is PHP like ‘Y-m-d’. If you use “short” or “medium” etc. local definitions are used. Internally ICU is used but “dateFormat” delivers ‘Y-m-d’. It’s also possible to communicate with ICU standard. If php extension “Intl” isn’t loaded it works partially.
At the moment I just started to test. If all works I will ask Yii core team to exchange it and I will send you the class also.
I have refactored formatter class. Details about changes see pull request:
Formatter class in ‘yii\base’ know supports localization with or without intl extension internally. Communication with methods happens with php format patterns (default) or with icu patterns if defined in parameter.
For your dateControl you could use yii::$app->formatter->getFormatPattern(‘date’|‘time’|‘datetime’). Attribute ‘dateFormat’ is still there for compatibility but it could be set to ‘short’, ‘medium’, ‘long’ or ‘full’.
There are unformat methods for almost all formats available.
At the moment samdark is reviewing the code in detail.
The PR is done under #3790. I still had some troubles with automatic testcases with PhpUnit. On my local platform it passes without error but on the travis it gives another result (date format with en-US only. It could be that they have another version of ICU library. Now it has been passed.