ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

php-将传统的日本时代转化为公历

2019-10-13 08:32:21  阅读:183  来源: 互联网

标签:php internationalization


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.

more info

我想在传统的日本日历和公历之间转换.

从此处使用日期格式:

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

标签:php,internationalization
来源: https://codeday.me/bug/20191013/1906545.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有