ksimka
ksimka

Reputation: 1430

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

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.

Upvotes: 4

Views: 5711

Answers (3)

flu
flu

Reputation: 14683

Since PHP 8.0 the IntlDatePatternGenerator is available (official Unicode documentation), which allows you to define base format pattern and automatically convert it to the "best" localized version.

Using that, a solution would be:

$now = new \DateTime('2016-02-01');

foreach (['en_US', 'ja_JA', 'ru_RU'] as $locale) {
    printf("%s:\n", $locale);
    $patternGenerator = new \IntlDatePatternGenerator($locale);

    foreach (['MMMd', 'MMMMd'] as $datetype) {
        $pattern = $patternGenerator->getBestPattern($datetype);
        printf("- %s\n",
            \IntlDateFormatter::formatObject(
                $now,
                $pattern,
                $locale
            )
        );
    }
}

This returns:

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

Unfortunately, it doesn't return 02/01 as the Japanese short version. This is magically done by IntlDateFormatter::MEDIUM when using normal formatting but cannot be done with IntlDatePatternGenerator::getBestPattern(). Though, maybe this could be achieved one day if DateTimePatternGenerator::replaceFieldTypes() is added to PHP as well.

But anyway, its very close, so I hope it may help others.

Upvotes: 0

Baishu
Baishu

Reputation: 1591

It's a bit ugly, but we can do something like this if you are OK defining the pattern for a limited number of locales and set a sensible default for the other locales.

$formatter = match ($lang) {
    'en' => new \IntlDateFormatter($lang, pattern: "MMMM d  'at'  h:mm a"),
    'es' => new \IntlDateFormatter($lang, pattern: "d 'de' MMMM, H:mm"),
    'ja' => new \IntlDateFormatter($lang, pattern: "M月d日 H:mm"),
    default => new \IntlDateFormatter($lang, \IntlDateFormatter::LONG, \IntlDateFormatter::NONE),
};

$date = $formatter->format($datetime);

Upvotes: 0

ksimka
ksimka

Reputation: 1430

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/

Upvotes: 6

Related Questions