English | 简体中文 | 繁體中文
查询

IntlDateFormatter::format()函数—用法及示例

「 根据指定的格式和语言环境格式化给定的日期和时间,并返回格式化后的字符串 」


函数名称:IntlDateFormatter::format()

适用版本:PHP 5 >= 5.3.0, PHP 7, PHP 8

函数描述:IntlDateFormatter::format() 函数根据指定的格式和语言环境格式化给定的日期和时间,并返回格式化后的字符串。

语法:string IntlDateFormatter::format ( mixed $value )

参数:

  • value:要格式化的日期和时间值。可以是一个 DateTime 对象,一个时间戳(自 Unix 纪元以来的秒数)或一个可以转换为时间戳的字符串。

返回值:返回格式化后的日期和时间字符串,如果格式化失败则返回 FALSE。

示例:

$formatter = new IntlDateFormatter(
    'en_US',
    IntlDateFormatter::FULL,
    IntlDateFormatter::NONE,
    'America/New_York',
    IntlDateFormatter::GREGORIAN
);

$date = new DateTime('2022-01-01 12:34:56');

echo $formatter->format($date); // 输出:Saturday, January 1, 2022

在上面的示例中,我们首先创建了一个 IntlDateFormatter 对象,指定了语言环境为美国英语('en_US'),日期格式为完整格式(IntlDateFormatter::FULL),时间格式为无(IntlDateFormatter::NONE),时区为纽约('America/New_York'),日历系统为公历(IntlDateFormatter::GREGORIAN)。

然后,我们创建了一个 DateTime 对象,表示日期和时间为 2022-01-01 12:34:56。

最后,我们使用 IntlDateFormatter::format() 函数将日期和时间格式化为字符串,并将其输出。输出结果为 "Saturday, January 1, 2022"。

补充纠错
热门PHP函数
分享链接