PHP Conference Japan 2024

IntlDateFormatter::create

datefmt_create

IntlDateFormatter::__construct

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

IntlDateFormatter::create -- datefmt_create -- IntlDateFormatter::__construct建立日期格式器

說明

物件導向風格

public static IntlDateFormatter::create(
    ?字串 $locale,
    整數 $dateType = IntlDateFormatter::FULL,
    整數 (int) $timeType = IntlDateFormatter::FULL,
    IntlTimeZone|DateTimeZone|字串 (string)|null $timezone = null,
    IntlCalendar|整數 (int)|null $calendar = null,
    ?字串 (string) $pattern = null
): ?IntlDateFormatter

物件導向風格 (建構子)

public IntlDateFormatter::__construct(
    ?字串 $locale,
    整數 $dateType = IntlDateFormatter::FULL,
    整數 (int) $timeType = IntlDateFormatter::FULL,
    IntlTimeZone|DateTimeZone|字串 (string)|null $timezone = null,
    IntlCalendar|整數 (int)|null $calendar = null,
    ?字串 (string) $pattern = null
)

程序式風格

datefmt_create(
    ?字串 $locale,
    整數 $dateType = IntlDateFormatter::FULL,
    整數 (int) $timeType = IntlDateFormatter::FULL,
    IntlTimeZone|DateTimeZone|字串 (string)|null $timezone = null,
    IntlCalendar|整數 (int)|null $calendar = null,
    ?字串 (string) $pattern = null
): ?IntlDateFormatter

建立日期格式器。

參數

locale (地區設定)

用於格式化或剖析的語系,或設為 null 以使用 ini 設定 intl.default_locale 中指定的值。

dateType (日期類型)

日期格式,由其中一個 IntlDateFormatter 常數 決定。預設值為 IntlDateFormatter::FULL

timeType (時間類型)

時間格式,由其中一個 IntlDateFormatter 常數 決定。預設值為 IntlDateFormatter::FULL

timezone (時區)

時區 ID。預設值(以及給定 null 時使用的值)是由 date_default_timezone_get() 返回的值,或者,如果適用,則為傳遞給 calendar 參數的 IntlCalendar 物件的時區。此 ID 必須是 ICU 資料庫上的有效識別碼,或是代表明確偏移量的 ID,例如 GMT-05:30

這也可以是一個 IntlTimeZoneDateTimeZone 物件。

calendar (曆法)

用於格式化或解析的曆法。預設值為 null,對應於 IntlDateFormatter::GREGORIAN。這可以是 IntlDateFormatter 曆法常數 之一,或是 IntlCalendar 物件。任何傳入的 IntlCalendar 物件都會被複製;它不會被 IntlDateFormatter 修改。這將決定使用的曆法類型(格里高利曆、伊斯蘭曆、波斯曆等),並且如果 timezone 參數給定 null,則也會決定使用的時區。

格式

格式化或解析時使用的選用格式。可能的格式記錄在 » https://unicode-org.github.io/icu/userguide/format_parse/datetime/

回傳值

建立的 IntlDateFormatter 物件,或失敗時回傳 null

錯誤/例外

如果 locale 無效,則會拋出 ValueError

更新日誌

版本 說明
8.4.0 如果 locale 無效,則會拋出 ValueError
8.1.0

參數 dateTypetimeType 現在是選用的。

範例

範例 #1 datefmt_create() 範例

<?php
$fmt
= datefmt_create( "en_US" ,IntlDateFormatter::FULL, IntlDateFormatter::FULL,
'America/Los_Angeles', IntlDateFormatter::GREGORIAN );
echo
"First Formatted output is ".datefmt_format( $fmt , 0);
$fmt = datefmt_create( "de-DE" ,IntlDateFormatter::FULL, IntlDateFormatter::FULL,
'America/Los_Angeles',IntlDateFormatter::GREGORIAN );
echo
"Second Formatted output is ".datefmt_format( $fmt , 0);

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

範例 #2 物件導向範例

<?php
$fmt
= new IntlDateFormatter( "en_US" ,IntlDateFormatter::FULL, IntlDateFormatter::FULL,
'America/Los_Angeles',IntlDateFormatter::GREGORIAN );
echo
"第一個格式化後的輸出為 ".$fmt->format(0);
$fmt = new IntlDateFormatter( "de-DE" ,IntlDateFormatter::FULL, IntlDateFormatter::FULL,
'America/Los_Angeles',IntlDateFormatter::GREGORIAN );
echo
"第二個格式化後的輸出為 ".$fmt->format(0);

$fmt = new IntlDateFormatter( "en_US" ,IntlDateFormatter::FULL, IntlDateFormatter::FULL,
'America/Los_Angeles',IntlDateFormatter::GREGORIAN ,"MM/dd/yyyy");
echo
"使用特定格式的第一個格式化後的輸出為 ".$fmt->format(0);
$fmt = new IntlDateFormatter( "de-DE" ,IntlDateFormatter::FULL, IntlDateFormatter::FULL,
'America/Los_Angeles',IntlDateFormatter::GREGORIAN , "MM/dd/yyyy");
echo
"使用特定格式的第二個格式化後的輸出為 ".$fmt->format(0);
?>

範例 #3 無效語系處理範例

<?php
try {
$fmt = new IntlDateFormatter(
'invalid_locale',
IntlDateFormatter::FULL,
IntlDateFormatter::FULL,
'dunno',
IntlDateFormatter::GREGORIAN,
);
} catch (
\Error $e) {
// ...
}
?>

以上範例會輸出

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

另請參閱

新增註記

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

daniel dot rhodes at warpasylum dot co dot uk
13 年前
應該要注意的是,傳遞給 IntlDateFormatter 建構函式的語系字串支援 UCA 關鍵字。因此,您可以執行例如以下操作,以日本年號年份輸出年份

<?php
$now
= new DateTime(); //DateTime 是 PHP 5.2.0 以後的核心類別

$formatter = new IntlDateFormatter('ja_JP', IntlDateFormatter::FULL,
IntlDateFormatter::FULL, 'Asia/Tokyo', IntlDateFormatter::GREGORIAN);

echo
'現在時間是:"' . $formatter->format($now) . '" (東京時間)' . "\n";
//上述程式碼會輸出 [現在時間是:"2011年8月19日金曜日 23時32分27秒JST" (東京時間)]

$formatter = new IntlDateFormatter('ja_JP@calendar=japanese', IntlDateFormatter::FULL,
IntlDateFormatter::FULL, 'Asia/Tokyo', IntlDateFormatter::TRADITIONAL);

echo
'現在時間是:"' . $formatter->format($now) . '" (東京時間)' . "\n";
//上述程式碼會輸出 [現在時間是:"平成23年8月19日金曜日 23時32分27秒JST" (東京時間)]
?>
mikko dot rantalainen at peda dot net
12 年前
文件說明:「時區:時區 ID,預設為系統預設值。」

「系統預設值」實際上僅指 Unix/Linux 系統上的「TZ」環境變數。它並不表示 PHP ini 設定或透過 date_default_timezone_set() 設定的值,也不是檔案「/etc/timezone」中的作業系統預設時區。
info at mobger dot de
1 年前
`$locale` 也可以包含日曆資訊

<?php
//...
$traditionalFormatter = new IntlDateFormatter(
$locale.'@calendar='.$calendar,
IntlDateFormatter::SHORT,
IntlDateFormatter::SHORT,
'Europe/Berlin',
IntlDateFormatter::TRADITIONAL,
'yyyy/MM/dd HH:mm:ss' // ICU 日期時間格式
);
//..
?>

要取得 `$calendar` 的允許值,請使用以下程式碼

<?php
$bundle
=new ResourceBundle('','ICUDATA');
$cnames=[];
$calendars=$bundle->get('calendar');
foreach(
$calendars as $n=>$v){
$cnames[]=$n;
}
echo (
print_r($cnames,true));

?>

結果如下:
陣列
(
[0] => buddhist(佛教曆)
[1] => chinese(農曆)
[2] => coptic(科普特曆)
[3] => dangi(檀紀)
[4] => default(預設)
[5] => ethiopic(衣索比亞曆)
[6] => ethiopic-amete-alem(衣索比亞阿梅特·阿萊姆曆)
[7] => gregorian(格里曆/公曆)
[8] => hebrew(希伯來曆)
[9] => indian(印度曆)
[10] => islamic(伊斯蘭曆)
[11] => islamic-civil(伊斯蘭民用曆)
[12] => japanese(日本曆)
[13] => persian(波斯曆)
[14] => roc(民國紀年)
)
匿名
6 年前
文件說明 $datetype 和 $timetype 也可以是 NULL,在這種情況下,將使用 ICU 的預設日期類型或時間類型。

但是當同時設定 (strict_types=1) 時,Intl 無法建立 IntlDateFormatter 類別,並返回錯誤「datefmt_create: unable to parse input parameters(無法解析輸入參數)」。
Patanjali
3 年前
要使用任何非格里曆(公曆),需要說明的是:

a. 語系必須採用 locale@calendar=calendar-name 的格式

b. 曆法必須使用 intlDateFormatter::TRADITIONAL 常數,或使用 a 中指定的語系建立的 intlCalendar 物件。

即使使用了 intlDateFormatter::TRADITIONAL 常數,日曆名稱也可以是 'gregorian',這使得程式碼方法更通用。
To Top