gpt4 book ai didi

php - 使用 IntlDateFormatter 转换非公历日期

转载 作者:可可西里 更新时间:2023-10-31 23:05:21 25 4
gpt4 key购买 nike

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

$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) 设置日期方法。

$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

关于php - 使用 IntlDateFormatter 转换非公历日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24042819/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com