How to format date with a IntlDateFormatter using MEDIUM or FULL datetype, but without a year?

对着背影说爱祢 提交于 2020-01-13 05:15:28

问题


First of all: I know that I can manually create a bunch of config files with corresponding patterns for every locale; actually I try to find a workaround with using only IntlDateFormatter.

I'll try to explain with examples.

<?php

$tz = new DateTimeZone('Europe/Moscow');
$now = time();
foreach (['en_US', 'ja_JA', 'ru_RU'] as $locale) {
    printf("%s:\n", $locale);

    foreach ([IntlDateFormatter::MEDIUM, IntlDateFormatter::LONG] as $datetype) {
        $formatter = new IntlDateFormatter($locale, $datetype, IntlDateFormatter::NONE);
        printf("- %s\n", $formatter->format($now));
    }
}

https://3v4l.org/iJOT4

This produces

en_US:
- Feb 1, 2016
- February 1, 2016
ja_JA:
- 2016/02/01
- 2016年2月1日
ru_RU:
- 1 февр. 2016 г.
- 1 февраля 2016 г.

And I need

en_US:
- Feb 1
- February 1
ja_JA:
- 02/01
- 2月1日
ru_RU:
- 1 февр.
- 1 февраля

The first idea was to extract a pattern for given locale and remove any 'y' and 'Y' letters. But as you can see, there is more than just a 4-digit year: all that commas, slashes, labels (like 'г.' and '年').

PS:

Actually, what I want from an ideal IntlDateFormatter implementation is a smart pattern, where all components are in their places, but I can configure what pattern to use for each component. Like: instead of 'd MMMM y г.' pattern for ru_RU formatter has a 'dmy' pattern, and for d component it has d pattern, m - MMMM and y - y г.. So I could say y component is a '' (empty string), and voila.

Also, if you are aware of any library that already does this — please, let me know.


回答1:


So, currently it's impossible. I've found that intl PHP extension simply lacks of DateTimePatternGenerator (http://userguide.icu-project.org/formatparse/datetime), which is exactly what I need.

The DateTimePatternGenerator class provides a way to map a request for a set of date/time fields, along with their width, to a locale-appropriate format pattern. The request is in the form of a “skeleton” which just contains pattern letters for the desired fields using the representation for the desired width. In a skeleton, anything other than a pattern letter is ignored, field order is insignificant, and there are two special additional pattern letters that may be used: 'j' requests the preferred hour-cycle type for the locale (it gets mapped to one of 'H', 'h', 'k', or 'K'); 'J' is similar but requests no AM/PM marker even if the locale’s preferred hour-cycle type is 'h' or 'K'.

For example, a skeleton of “MMMMdjmm” might result in the following format patterns for different locales:

locale | format pattern for skeleton “MMMMdjmm” | example
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
en_US    "MMMM d  'at'  h:mm a"                   April 2 at 5:00 PM
es_ES    "d 'de' MMMM, H:mm"                      2 de abril, 17:00
ja_JP    "M月d日 H:mm"                              4月2日 17:00

Also, I've found that HHVM has implemented it — https://github.com/facebook/hhvm/commit/bc84daf7816e4cd268da59d535dcadfc6cf01085 . I hope one day this will be ported to PHP.

UPD: I've written a huge post on the problem — https://blog.ksimka.com/a-long-journey-to-formatting-a-date-without-a-year-internationally-with-php/



来源:https://stackoverflow.com/questions/35129924/how-to-format-date-with-a-intldateformatter-using-medium-or-full-datetype-but-w

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!