使用IntlDateFormatter转换非格里高里历日期 [英] Converting non-Gregorian dates using IntlDateFormatter

查看:99
本文介绍了使用IntlDateFormatter转换非格里高里历日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用IntlDateFormatter将非格里高利历日期转换为其他日历类型. 我想将"1392-01-02"persian转换为islamic日历.我尝试了以下代码,但它不转换日历:

How should I convert a non-Gregorian date to other calender types using IntlDateFormatter. I want convert "1392-01-02" from persian to islamic calender. I tried the following code but it does not convert the calendar:

$formatter = IntlDateFormatter::create('en_US@calendar=persian', IntlDateFormatter::FULL,
    IntlDateFormatter::FULL, 'Asia/Tokyo',IntlDateFormatter::TRADITIONAL,'yy-MM-dd');
$formatter->setCalendar(IntlDateFormatter::GREGORIAN);
echo $formatter->format($formatter->parse("1392-2-31"));

推荐答案

定义要转换为IntlCalendar实例的时间,并使用其set($year,$month,$day)方法设置日期.

Define the time you're converting as an instance of IntlCalendar and set the date using its set($year,$month,$day) method.

$time = IntlCalendar::createInstance("Asia/Tokyo", "en_US@calendar=persian");
$time->set(1392,2,31);

//alternative way to define time using parse

/**
These constants are used to specify different formats in the constructor for DateType 
and TimeType.

IntlDateFormatter::NONE (integer)
Do not include this element
IntlDateFormatter::FULL (integer)
Completely specified style (Tuesday, April 12, 1952 AD or 3:30:42pm PST)
IntlDateFormatter::LONG (integer)
Long style (January 12, 1952 or 3:30:32pm)
IntlDateFormatter::MEDIUM (integer)
Medium style (Jan 12, 1952)
IntlDateFormatter::SHORT (integer)
Most abbreviated style, only essential data (12/13/52 or 3:30pm)
*/

$fmt = new IntlDateFormatter(
    'en_US@calendar=persian',
    IntlDateFormatter::SHORT, //date format
    IntlDateFormatter::NONE, //time format
    'Asia/Tokyo',
    IntlDateFormatter::TRADITIONAL
);

$time = $fmt->parse('3/31/1392 AP');

$formatter = IntlDateFormatter::create("en_US@calendar=islamic",
  IntlDateFormatter::FULL, 
  IntlDateFormatter::FULL,
  'Asia/Tokyo', 
  IntlDateFormatter::TRADITIONAL);

print $formatter->format($time);
//Friday, Shaʻban 13, 1434 AH at 8:52:23 AM Japan Standard Time

这篇关于使用IntlDateFormatter转换非格里高里历日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆