Yii2 Locale Date Formatting With Datetimepicker

Hi

In Yii 1.1 we could have the locale formats with :


Yii::app()->locale->getDateTimeFormat('short')

or


Yii::app()->locale->getDateFormat('short')

and


Yii::app()->locale->getTimeFormat('short')

How to have these methods with Yii2 ?

In fact I want to put it the datePicker or dateTimePicker widget. But I don’t understand how to do this !

How to have with Yii2 the locale dateFormat and the locale timeFormat for JS ?




$(datepickerSelector).datetimepicker({

              dateFormat: "dd/mm/yy", //I want here the locale date format ^^

              timeFormat: "HH:mm"

});



And after validating the form, how to convert the value to timestamp in my model ?

Thank you ! :)

As per ICU specs … For example:




echo \Yii::t('app', '{0, date, short}', time());

echo \Yii::t('app', '{0, time, short}', time());



Read more about it in the docs.

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.

KartiK V, when using yii2-datecontrol with widgets/datepicker, how can I set ‘autoclose’ => true ?

Thanks!

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

    ]

]);



Thanks Kartik, it works. And next time I’ll post in the right place.

No problem. Glad that helped. Note these links for your future reference for any issues:

  • yii2-widgets extension home

  • yii2-widgets forum

  • yii2-widgets github page

  • yii2-widgets docs & demo

Thank you for your reply. I saw these methods in the doc :)

But in my case I just want to get the locale date format like this :


Yii::app()->locale->getDateFormat('short')

I don’t find this method :(

@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.

Erik_r thanks for the post. Will await to see this enhancement request in the core framework.

@Kartik V:

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.

@Erik - thanks will review and await the PR for merge in the core.

Hello Kartik

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.