首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用IntlDateFormatter转换非公历日期

使用IntlDateFormatter转换非公历日期
EN

Stack Overflow用户
提问于 2014-06-04 16:30:58
回答 1查看 1.5K关注 0票数 4

如何使用IntlDateFormatter将非公历日期转换为其他日历类型。我希望将"1392-01-02"persian转换为islamic日历。我尝试了以下代码,但它没有转换日历:

代码语言:javascript
复制
$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"));
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-06-06 23:50:56

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

代码语言:javascript
复制
$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
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24042819

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档