函式名稱: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"。
熱門工具排行榜