将传统的日本纪元年转换为公历年
Converting traditional Japanese era year into Gregorian year
The traditional Japanese calendar consists of eras based on the
reigning emperors. The imperial date format is required for some
government documents and applications. For example, until Jan 1, 2002,
the Japanese patent office used emperor dates.
我想在传统的日本日历和公历之间进行转换。
使用此处的日期格式:
http://www.icu-project.org/apiref/icu4c/classSimpleDateFormat.html#details
与 Internationalization Functions in PHP.
我开发了这个脚本:
/**
* 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
但是,如果你看一下我的代码,你会发现我需要日本天皇的编号才能生效。
我想知道是否可以转换这个字符串:
'平成27'
直接进入正确的公历年(2015)。
我知道我可以通过制作一个字符串数组 => EmperorNumber 来做到这一点,但我想知道是否有更好的正确方法。
谢谢指教。
你只需要使用 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";
给出:
Age in Japanese: 平成24
And back: AD2012
The traditional Japanese calendar consists of eras based on the reigning emperors. The imperial date format is required for some government documents and applications. For example, until Jan 1, 2002, the Japanese patent office used emperor dates.
我想在传统的日本日历和公历之间进行转换。
使用此处的日期格式:
http://www.icu-project.org/apiref/icu4c/classSimpleDateFormat.html#details
与 Internationalization Functions in PHP.
我开发了这个脚本:
/**
* 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
但是,如果你看一下我的代码,你会发现我需要日本天皇的编号才能生效。
我想知道是否可以转换这个字符串:
'平成27'
直接进入正确的公历年(2015)。
我知道我可以通过制作一个字符串数组 => EmperorNumber 来做到这一点,但我想知道是否有更好的正确方法。
谢谢指教。
你只需要使用 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";
给出:
Age in Japanese: 平成24 And back: AD2012