Php 将传统的日本纪年转换为公历年

Php 将传统的日本纪年转换为公历年,php,internationalization,Php,Internationalization,传统的日本历法由基于日期的纪元组成 在位的皇帝。某些情况下需要使用英制日期格式 政府文件和申请。例如,到2002年1月1日,, 日本专利局使用天皇日期 我想在传统的日本历法和公历之间做一个转换 使用此处的日期格式: 而且是用这个 我开发了这个脚本: /** * Convert japanese year (traditional) to gregorian calendar * * @author Gerard Brull <gbblanes@gmail.com> *

传统的日本历法由基于日期的纪元组成 在位的皇帝。某些情况下需要使用英制日期格式 政府文件和申请。例如,到2002年1月1日,, 日本专利局使用天皇日期

我想在传统的日本历法和公历之间做一个转换

使用此处的日期格式:

而且是用这个

我开发了这个脚本:

/**
 * Convert japanese year (traditional) to gregorian calendar
 * 
 * @author  Gerard Brull <gbblanes@gmail.com>
 * @version 0.1 29/01/2015 (in gregorian calendar :P)
 */

if (version_compare(PHP_VERSION, '5.3.0', '<')) {
    die('we need php 5.3.0 or later');
}

if (!class_exists('IntlDateFormatter')) {
    die('we need php_intl extension.');
}

//----------------------------------------------------------------------
// CONVERT JAPANESE YEAR ERA IN GREGORIAN CALENDAR
//----------------------------------------------------------------------


$cal = IntlCalendar::createInstance(null,'ja_JP@calendar=japanese');

//You can find the era number here: http://demo.icu-project.org/icu-bin/locexp?_=ja_JP&d_=en&calendar=japanese
$cal->set(IntlCalendar::FIELD_ERA, 235); //Heisei (平成)

$cal->set(IntlCalendar::FIELD_YEAR, 27); //year of the era
$cal->clear(IntlCalendar::FIELD_HOUR_OF_DAY);
$cal->clear(IntlCalendar::FIELD_MINUTE);
$cal->clear(IntlCalendar::FIELD_SECOND);
$cal->clear(IntlCalendar::FIELD_MILLISECOND);

echo 'Year in Gregorian calendar ' . $cal->get(IntlCalendar::FIELD_YEAR_WOY) . ' | ' ;
//Result: Year in Gregorian calendar 2015 | 



//----------------------------------------------------------------------
// CONVERT GREGORIAN CALENDAR (NOW) IN JAPANESE YEAR ERA
//----------------------------------------------------------------------

$now = new DateTime();

$formatter = new IntlDateFormatter(
    'ja_JP@calendar=japanese',
    IntlDateFormatter::FULL,
    IntlDateFormatter::FULL,
    'Europe/Madrid',
    IntlDateFormatter::TRADITIONAL,
    'Gy' //Age and year (regarding the age)
);

echo 'Age in Japanese: '. $formatter->format($now);
//Result: Age in Japanese: 平成27
直接转入正确的公历年(2015年)

我知道我可以通过创建一个字符串数组=>emperNumber来实现,但我想知道是否有更好的方法


感谢您的建议。

您只需使用
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";

你能给我解释一下这句话吗:$gregCalendar->setTime($time*1000);为什么“*1000”@GerardBrull这是因为->解析实现将它从ICU得到的结果除以1000并将其截断为整数,而
IntlCalendar::setTime
保留了ICU语义。
<?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";
Age in Japanese: 平成24 And back: AD2012