PHP Conference Japan 2024

IntlDateFormatter::formatObject

datefmt_format_object

(PHP 5 >= 5.5.0, PHP 7, PHP 8, PECL intl >= 3.0.0)

IntlDateFormatter::formatObject -- datefmt_format_object格式化物件

描述

物件導向風格

public static IntlDateFormatter::formatObject(IntlCalendar|DateTimeInterface $datetime, array|int|string|null $format = null, ?string $locale = null): string|false

程序式風格

datefmt_format_object(IntlCalendar|DateTimeInterface $datetime, array|int|string|null $format = null, ?string $locale = null): string|false

此函數允許格式化 IntlCalendarDateTime 物件,而無需先明確建立 IntlDateFormatter 物件。

將會建立的臨時 IntlDateFormatter 將會從傳入的物件中取得時區。PHP 捆綁的時區資料庫將不會使用,而是會使用 ICU 的時區資料庫。因此,DateTime 物件中使用的時區識別碼也必須存在於 ICU 的資料庫中。

參數

datetime

類型為 IntlCalendarDateTime 的物件。將會使用物件中的時區資訊。

format

如何格式化日期/時間。這可以是具有兩個元素的 array(第一個是日期樣式,第二個是時間樣式,這些樣式是常數 IntlDateFormatter::NONEIntlDateFormatter::SHORTIntlDateFormatter::MEDIUMIntlDateFormatter::LONGIntlDateFormatter::FULL 之一)、值為其中一個常數的 int(在這種情況下,它將同時用於時間和日期),或是具有 » ICU 文件中描述格式的 string。如果為 null,將會使用預設樣式。

locale

要使用的地區設定,或使用 預設地區設定,則為 null

傳回值

結果的字串,或失敗時為 false

範例

範例 1 IntlDateFormatter::formatObject() 範例

<?php
/* 預設時區無關;時區取自物件 */
ini_set('date.timezone', 'UTC');
/* 預設地區設定取自此 ini 設定 */
ini_set('intl.default_locale', 'fr_FR');

$cal = IntlCalendar::fromDateTime("2013-06-06 17:05:06 Europe/Dublin");
echo
"預設:\n\t",
IntlDateFormatter::formatObject($cal),
"\n";

echo
"long \$format (full):\n\t",
IntlDateFormatter::formatObject($cal, IntlDateFormatter::FULL),
"\n";

echo
"array \$format (none, full):\n\t",
IntlDateFormatter::formatObject($cal, array(
IntlDateFormatter::NONE,
IntlDateFormatter::FULL)),
"\n";

echo
"string \$format (d 'of' MMMM y):\n\t",
IntlDateFormatter::formatObject($cal, "d 'of' MMMM y", 'en_US'),
"\n";

echo
"使用 DateTime:\n\t",
IntlDateFormatter::formatObject(
new
DateTime("2013-09-09 09:09:09 Europe/Madrid"),
IntlDateFormatter::FULL,
'es_ES'),
"\n";

以上範例會輸出

default:
    6 juin 2013 17:05:06
long $format (full):
    jeudi 6 juin 2013 17:05:06 heure d’été irlandaise
array $format (none, full):
    17:05:06 heure d’été irlandaise
string $format (d 'of' MMMM y):
    6 of June 2013
with DateTime:
    lunes, 9 de septiembre de 2013 09:09:09 Hora de verano de Europa central

新增註解

使用者貢獻的註解 4 個註解

1
匿名
1 年前
`format` 與 static `formatObject`

** `formatObject` 並不比較慢!(在 PHP 5.5 上)**

請使用 `format` 方法或靜態的 `formatObject`。

由於 `formatObject` 使用提供的模式執行 `new IntlDateFormatter` 的工作,因此實例化程式碼行必須包含在迴圈中!

另一個被揭穿的錯誤測試!

php -v
PHP 5.5.26-1+deb.sury.org~precise+1 (cli) (建置於:Jun 15 2015 10:04:01)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2015 Zend Technologies
使用 Zend OPcache v7.0.6-dev, Copyright (c) 1999-2015, by Zend Technologies
使用 Xdebug v2.3.2, Copyright (c) 2002-2015, by Derick Rethans

<?php
date_default_timezone_set
('America/Los_Angeles');
$n = 3000;

$dt = new DateTime('2015-01-03 12:32:44');

$time[] = microtime(true);
for(
$i=0;$i<$n;$i++) {
$a = IntlDateFormatter::formatObject($dt, 'MMMM dd', 'hu_HU');
}
echo
"$a\n";
$time[] = microtime(true);
for(
$i=0;$i<$n;$i++) {
$df = new IntlDateFormatter('hu_HU', IntlDateFormatter::SHORT, IntlDateFormatter::NONE, null, null, 'MMMM dd');
$a = $df->format($dt);
}
echo
"$a\n";
$time[] = microtime(true);

for(
$j=1;$j<count($time);$j++) {
printf("%fs\n", $time[$j]-$time[$j-1]);
}
?>

`formatObject` : 0.336579 秒
`format` : 0.391158 秒
2
raf at sns dot pm
2 年前
如果您想要根據特定格式和本地語言格式化日期,這裡有格式代碼的參考連結,我沒有在文件中直接找到它
https://unicode-org.github.io/icu/userguide/format_parse/datetime/#date-field-symbol-table
以下是使用 DateTime 物件的測試

<?php

// 必須由伺服器需求
date_default_timezone_set( 'Europe/Paris' );

// 實例化一個新的 DateTime 物件
$dateTimeObj = new DateTime('now', new DateTimeZone('Europe/Paris'));

// 使用特定的格式格式化日期
// 這 3 個參數是 [ DateTime 物件, ICU 格式, 地區碼字串 ]
$dateFromatted = IntlDateFormatter::formatObject( $dateTimeObj, "eee d MMMM y à HH:mm", 'fr' );

// 測試:
echo ucwords($dateFromatted);
// 輸出:Jeu. 7 Avril 2022 à 04:36 // 依照我想要的方式格式化

?>
-3
ferenczi dot krisztian at gmail dot com
9 年前
`format` 與 static `formatObject`

`formatObject` 比較慢! `format` 快了 10-13 倍以上!(在 PHP 5.5 上)請使用 `format` 方法而不是靜態的 `formatObject`。

php -v
PHP 5.5.26-1+deb.sury.org~precise+1 (cli) (建置於:Jun 15 2015 10:04:01)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2015 Zend Technologies
使用 Zend OPcache v7.0.6-dev, Copyright (c) 1999-2015, by Zend Technologies
使用 Xdebug v2.3.2, Copyright (c) 2002-2015, by Derick Rethans

<?php
$n
= 3000;

$dt = new \DateTime('2015-01-03 12:32:44');
$df = new IntlDateFormatter('hu_HU', IntlDateFormatter::SHORT, IntlDateFormatter::NONE);
$df->setPattern('MMMM dd');

$time[] = microtime(true);
for(
$i=0;$i<$n;$i++) {
$a = IntlDateFormatter::formatObject($dt, 'MMMM dd', 'hu_HU');
}
echo
"$a\n";
$time[] = microtime(true);
for(
$i=0;$i<$n;$i++) {
$a = $df->format($dt);
}
echo
"$a\n";
$time[] = microtime(true);

for(
$j=1;$j<count($time);$j++) {
printf("%fs\n", $time[$j]-$time[$j-1]);
}
?>

`formatObject` : 0.458248 秒
`format` : 0.033759 秒
-3
sebastian at huehnerhose dot de
7 年前
在 php7.1 上仍然比較慢,但不再那麼顯著,在這裡我得到大約慢 5 倍
To Top