2024 年 PHP Conference Japan

IntlDateFormatter::format

datefmt_format

(PHP 5 >= 5.3.0, PHP 7, PHP 8, PECL intl >= 1.0.0)

IntlDateFormatter::format -- datefmt_format將日期/時間值格式化為字串

說明

物件導向風格

public IntlDateFormatter::format(IntlCalendar|DateTimeInterface|陣列|字串|整數|浮點數 $datetime): 字串|false

程序風格

將時間值格式化為字串。

參數

formatter

日期格式化器資源。

datetime

要格式化的值。這可以是 DateTimeInterface 物件、IntlCalendar 物件、代表自起始時間以來(可能是分數)秒數的 數值 類型,或是 localtime() 輸出的格式 陣列

如果傳入 DateTimeIntlCalendar 物件,則不會考慮其時區。該物件將使用格式化器設定的時區進行格式化。如果要使用要格式化的物件的時區,則必須事先使用物件的時區呼叫 IntlDateFormatter::setTimeZone()。或者,可以使用靜態函數 IntlDateFormatter::formatObject()

回傳值

格式化的字串,如果發生錯誤,則為 false

更新日誌

版本 說明
7.1.5 新增了對 datetime 參數提供一般 DateTimeInterface 物件的支援。以前,僅支援正確的 DateTime 物件。
PECL intl 3.0.0 新增了對 datetime 參數提供 IntlCalendar 物件的支援。

範例

範例 #1 datefmt_format() 範例

<?php
$fmt
= datefmt_create(
'en_US',
IntlDateFormatter::FULL,
IntlDateFormatter::FULL,
'America/Los_Angeles',
IntlDateFormatter::GREGORIAN
);
echo
'第一個格式化後的輸出是 ' . datefmt_format($fmt, 0);

$fmt = datefmt_create(
'de-DE',
IntlDateFormatter::FULL,
IntlDateFormatter::FULL,
'America/Los_Angeles',
IntlDateFormatter::GREGORIAN
);
echo
'第二個格式化後的輸出是 ' . datefmt_format($fmt, 0);

$fmt = datefmt_create(
'en_US',
IntlDateFormatter::FULL,
IntlDateFormatter::FULL,
'America/Los_Angeles',
IntlDateFormatter::GREGORIAN,
'MM/dd/yyyy'
);
echo
'使用模式的第一個格式化輸出是 ' . datefmt_format($fmt, 0);

$fmt = datefmt_create(
'de-DE',
IntlDateFormatter::FULL,
IntlDateFormatter::FULL,
'America/Los_Angeles',
IntlDateFormatter::GREGORIAN,
'MM/dd/yyyy'
);
echo
"使用模式的第二個格式化輸出是 " . datefmt_format($fmt, 0);
?>

範例 #2 物件導向範例

<?php
$fmt
= new IntlDateFormatter(
'en_US',
IntlDateFormatter::FULL,
IntlDateFormatter::FULL,
'America/Los_Angeles',
IntlDateFormatter::GREGORIAN
);
echo
'First Formatted output is ' . $fmt->format(0);

$fmt = new IntlDateFormatter(
'de-DE',
IntlDateFormatter::FULL,
IntlDateFormatter::FULL,
'America/Los_Angeles',
IntlDateFormatter::GREGORIAN
);
echo
'Second Formatted output is ' . $fmt->format(0);

$fmt = new IntlDateFormatter(
'en_US',
IntlDateFormatter::FULL,
IntlDateFormatter::FULL,
'America/Los_Angeles',
IntlDateFormatter::GREGORIAN,
'MM/dd/yyyy'
);
echo
'First Formatted output with pattern is ' . $fmt->format(0);

$fmt = new IntlDateFormatter(
'de-DE',
IntlDateFormatter::FULL,
IntlDateFormatter::FULL,
'America/Los_Angeles',
IntlDateFormatter::GREGORIAN,
'MM/dd/yyyy'
);
echo
'Second Formatted output with pattern is ' . $fmt->format(0);
?>

以上範例將輸出

First Formatted output is Wednesday, December 31, 1969 4:00:00 PM PT
Second Formatted output is Mittwoch, 31. Dezember 1969 16:00 Uhr GMT-08:00
First Formatted output with pattern is 12/31/1969
Second Formatted output with pattern is 12/31/1969

範例 #3 使用 IntlCalendar 物件

<?php
$tz
= reset(iterator_to_array(IntlTimeZone::createEnumeration('FR')));
$formatter = IntlDateFormatter::create(
'fr_FR',
IntlDateFormatter::FULL,
IntlDateFormatter::FULL,
$tz,
IntlDateFormatter::GREGORIAN
);

$cal = IntlCalendar::createInstance($tz, '@calendar=islamic-civil');
$cal->set(IntlCalendar::FIELD_MONTH, 8); //設定為第 9 個月,齋戒月
$cal->set(IntlCalendar::FIELD_DAY_OF_MONTH, 1); //設定為第一天
$cal->clear(IntlCalendar::FIELD_HOUR_OF_DAY);
$cal->clear(IntlCalendar::FIELD_MINUTE);
$cal->clear(IntlCalendar::FIELD_SECOND);
$cal->clear(IntlCalendar::FIELD_MILLISECOND);

echo
"在這個伊斯蘭年中,齋戒月開始/將開始於:\n\t",
$formatter->format($cal), "\n";

//使用的是格式化器的時區:
$formatter->setTimeZone('Asia/Tokyo');
echo
"更改時區後:\n\t",
$formatter->format($cal), "\n";

以上範例將輸出

In this islamic year, Ramadan started/will start on:
    mardi 9 juillet 2013 19:00:00 heure avancée d’Europe centrale
After changing timezone:
    mercredi 10 juillet 2013 02:00:00 heure normale du Japon

另請參閱

新增註記

使用者貢獻的註記 4 則註記

mail dot dennisbecker at gmail dot com
11 年前
您應該知道 PHP 的 IntlDateFormatter 類別使用的是 ISO 日期格式碼,而不是 PHP 的 date() 格式碼。在我看來,這一點並沒有很清楚地說明。

您可以在 http://framework.zend.com/manual/1.12/en/zend.date.constants.html#zend.date.constants.selfdefinedformats 找到一份不錯的 ISO 格式碼列表,也應該在此處新增一份這樣的列表。
con at bartrail dot de
13 年前
我希望這能為將來必須在開發和正式平台上使用不同 PHP 版本的人節省一些時間。

使用*自訂模式*格式化 DateTime 物件時,請務必使用時間戳記傳遞給 IntlDateFormatter::format,以便在不同 PHP 版本上正常運作。

PHP 版本 5.3.5-1ubuntu7.2(我的開發機器)範例
<?php
$date
= new \DateTime();

$dateFormatter = \IntlDateFormatter::create(
\Locale::getDefault(),
\IntlDateFormatter::NONE,
\IntlDateFormatter::NONE,
\date_default_timezone_get(),
\IntlDateFormatter::GREGORIAN,
'EEEE'
);

var_dump($dateFormatter->format($date)); // string(6) "Monday"
?>

PHP 版本 5.3.2-1ubuntu4.9(正式伺服器)範例
<?php
// 與上述相同的格式

var_dump($dateFormatter->format($date)); // bool(false)
?>

使用 $dateFormatter->format($date->getTimestamp()) 時,您將始終取得格式化且在地化的字串,而不是 false。
alex
14 年前
請務必注意,時區轉換會在預設時區 (date_default_timezone_set()) 和您在建構函式 (或 datefmt_create()) 中傳遞的時區之間進行。

如果您將日期以 UTC 時間插入資料庫,請確保也將 date_default_timezone_set 設定為 UTC(或任何其他時區,但它們必須相同)。一旦您呼叫 ::format,您將獲得轉換後的時間。
To Top