Date Formatting Two Year Digits Instead Of Four

Yii2 "i18n\formatter" uses PHP extension "intl" which use ICU standard. Standard date format or date-time format is defined as "short".

“IntlDateformatter” class delivers with standard “short” a two digit year date (eg. "14-10-14 [iso] or “14.10.14” [german format]. Such a date format isn’t usual and usful on application gui. I want to have a four digit year like “2014-10-14” [iso] or “14.10.2014” [german]. It’s better readable.

Further in datetime format the seconds are missing. ICU standard is "14-10-14 15:01". I added the seconds also.

To change this we only need some code lines in method "init" of formatter class. Afterwards existing "asDate" method delivers a four digit year.




        //Erik's Code to override short date format ICU delivers example 15.04.14 instead of 15.04.2014

        If ($this->dateFormat === 'short'){

            $formatter = new IntlDateFormatter(Yii::$app->language, IntlDateFormatter::SHORT, IntlDateFormatter::NONE);

            $this->dateFormat = preg_replace('<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/sad.gif' class='bbc_emoticon' alt=':(' />^|[^y])y{2,2}([^y]|$):', '$1yyyy$2', $formatter->getPattern());

            // pattern: 'dd.MM.yyyy' for DE or 'yyyy-MM-dd' for iso

        }

        If ($this->datetimeFormat === 'short'){

            $formatter = new IntlDateFormatter(Yii::$app->language, IntlDateFormatter::SHORT, IntlDateFormatter::SHORT);

            $this->datetimeFormat = preg_replace('<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/sad.gif' class='bbc_emoticon' alt=':(' />^|[^y])y{2,2}([^y]|$):', '$1yyyy$2', $formatter->getPattern()).':ss';

            // pattern: 'dd.MM.yyyy HH:mm:ss' for DE or 'yyyy-MM-dd HH:mm:ss' for iso

        }



This code replaces "SHORT" in $this->dateFormat by the correct pattern. The "asDate" method set this pattern by using "$formatter->setPattern($format)" ($format = $this-dateFormat).

I use an inherited formatter class and override the init method.

What is your opinion? Should this four digit date form not be supported by yii2 core?

@Erik_r good inputs. But do you have any idea, why your suggestion is not already included in ICU standard?

@Kartik: I don’t know why. I have checked some documents about ICU in internet. It’s really like this and I’m not the only one who isn’t happy with this behavior. I found a similar work around in another programming language.

By the way your DateControl is fantastic. I will use it. It provides more then I had as an idea. I have seen that you use yii\base\formatter class. If you want to have I18N able application you should use yii\i18n\formatter which is aware about the language. But your DateControl will fail, if there is not date pattern specified because you read formatters dateFormat. In I18N it will be "short" or in my modified case "dd.mm.yyyy".

The situation about formatter class isn’t satisfying in yii2. That’s way there is an issue in which they write about “refactoring”. see: https://github.com/yiisoft/yii2/issues/2359

Yii2 writes in documentation that formatter is newly ICU based but same class in old fashion is in yii\base. Which one should be used? Both works with different formats. It’s not clear which one will maintained in future? etc?

In yii\i18n\formatter there should be two functions like “getPatternICU” --> delivers eg. “dd-mm-yyyy” and one like “getPatternPhp” --> converts ICU to “d-M-Y”. If your DateControl could use “i18n” date will be formatted dynamically concerning the application language and don’t have to be hard coded in config file.

Erik - thanks that you liked the DateControl package. I also personally use it most, amongst my various other code.

Can you please post the changes/enhancements you suggested as a req on github… it would be great to track and close.

Kartik: It’s not an enhancement. It’s the issue that yii has different date patterns like PHP, ICU, JavaScript which must be converted. You also did it in your datePicker class. This converting tools should be in yii\i18n\formatter or interimistic in an inherited class.

I will open an issue in your gitHub if I have time. But I will need some days because I have another important work at the moment.

Ah… ok… yes I understand… I did do the date conversion helpers from PHP to JavaScript and vice versa for the JQuery date/time widgets. These are available in the DateControl package also directly for use (if you set it to use these widgets).

On the other topics like translations – it needs some enhancements probably which I will wait till you submit your thoughts as well.