函数名称:IntlDateFormatter::setCalendar()
适用版本:PHP 5 >= 5.5.0, PHP 7, PHP 8
函数描述:设置IntlDateFormatter对象的日历类型。
用法:
bool IntlDateFormatter::setCalendar ( mixed $calendar )
参数:
$calendar
:要设置的日历类型。可以是以下常量之一:IntlDateFormatter::TRADITIONAL
:传统的日历类型。IntlDateFormatter::GREGORIAN
:公历类型。
返回值:
- 成功时返回
true
,失败时返回false
。
示例:
$formatter = new IntlDateFormatter('en_US', IntlDateFormatter::SHORT, IntlDateFormatter::NONE);
// 设置日历类型为传统
$formatter->setCalendar(IntlDateFormatter::TRADITIONAL);
echo $formatter->format(time()); // 输出:12/31/21
// 设置日历类型为公历
$formatter->setCalendar(IntlDateFormatter::GREGORIAN);
echo $formatter->format(time()); // 输出:12/31/2021
以上示例创建了一个IntlDateFormatter
对象,并通过setCalendar()
方法设置了日历类型。首先,设置日历类型为传统(IntlDateFormatter::TRADITIONAL
),然后使用format()
方法将当前时间格式化为短日期字符串。接着,将日历类型设置为公历(IntlDateFormatter::GREGORIAN
),再次使用format()
方法将当前时间格式化为短日期字符串。最后,分别输出了两个不同日历类型下的日期格式化结果。