Converting traditional Japanese era year into Gregorian year

前端 未结 1 1403
余生分开走
余生分开走 2021-01-25 14:51

The traditional Japanese calendar consists of eras based on the reigning emperors. The imperial date format is required for some government documents and appl

相关标签:
1条回答
  • 2021-01-25 15:24

    You just have to use IntlDateFormatter::parse:

    <?php
    $formatter = new IntlDateFormatter(
        'ja_JP@calendar=japanese',
        IntlDateFormatter::FULL,
        IntlDateFormatter::FULL,
        'Europe/Madrid',
        IntlDateFormatter::TRADITIONAL,
        'Gy' //Age and year (regarding the age)
    );
    $r = $formatter->format(strtotime('2012-01-01 Europe/Madrid'));
    echo "Age in Japanese: $r\n";
    $time = $formatter->parse($r);
    $gregCalendar = IntlCalendar::createInstance('Europe/Madrid', 'ja_JP');
    $gregCalendar->setTime($time * 1000);
    $r2 = IntlDateFormatter::formatObject($gregCalendar, 'Gy');
    echo "And back: $r2\n";
    

    gives:

    Age in Japanese: 平成24
    And back: AD2012
    
    0 讨论(0)
提交回复
热议问题