1

I want to convert

令和元年8月 = 2019年8月

Ref https://www.conservapedia.com/Japanese_dates

Here I get these

enter image description here

For this I tried code like below here "平成31年8月" returning 2019 but according to ref it should 令和元年8月 Please suggest me is there any solution so I can set my code like reference..

$formatter = new IntlDateFormatter(
    'ja_JP@calendar=japanese',
    IntlDateFormatter::FULL,
    IntlDateFormatter::FULL,
    'Europe/Madrid',
    IntlDateFormatter::TRADITIONAL,
    'Gy年M月'
);
$ts = $formatter->parse('令和元年8月');
//$ts = $formatter->parse('平成31年8月');
var_dump($ts, date('Y-m', $ts));

___FIDDLE___

Lemon Kazi
  • 3,308
  • 2
  • 37
  • 67

1 Answers1

3

There seem to be two issues.

  1. Japanese era system does not switch by year, but by a date in a year

平成(Heisei) ended at 2019-04-30, 令和(Reiwa) began at 2019-05-01, so the year-to-year table you referred is not complete if you need to convert specific date in a switching year such like 2019.

For example, both January 平成31 and December 令和1 are AD 2019. So when you only convert year part, they would show the same result.

  1. PHP does not always have up-to-date table inside

On my local, "$formatter->parse('令和元年8月');" returned me 1970-01, the Unix epoch time probably coming from null value. This happens because my using PHP yet does not know that Japanese era changed to 令和.

IntlDateFormatter is in pecl php_intl extension, which calls ICU library. ICU library supports the new era name 令和 at its verion 64.2.

You may check your phpinfo() with "ICU version" and if that is smaller than 64.2, it won't convert 令和 properly.

$ php --info | grep "ICU version"
ICU version => 61.1

If you can not find 64.2+ on your latest available PHP, you may have to compile intl extension with later ICU library yourself.

akky
  • 2,818
  • 21
  • 31