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.
I would like to make a conversion between the traditional Japanese calendar and the Gregorian one.
Using the date format from here:
http://www.icu-project.org/apiref/icu4c/classSimpleDateFormat.html#details
And with the Internationalization Functions in PHP.
I developed this script:
/**
* 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
However, if you look on my code, you can see that I need the number of the Japanese emperor to make it work.
And i would like to know if it's possible to convert this string:
'平成27'
Into the correct Gregorian year (2015) directly.
I know I can do it by making an Array of Strings => EmperorNumber but I would like to know if there is a better proper way.
Thanks in advise.
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