IntlDateFormatter::formatObject
datefmt_format_object
(PHP 5 >= 5.5.0, PECL intl >= 3.0.0)
IntlDateFormatter::formatObject -- datefmt_format_object — Formats an object
Описание
Объектно-ориентированный стиль
$object
[, mixed $format
= NULL
[, string $locale
= NULL
]] )Процедурный стиль
$object
[, mixed $format
= NULL
[, string $locale
= NULL
]] )This function allows formatting an IntlCalendar or DateTime object without first explicitly creating a IntlDateFormatter object.
The temporary IntlDateFormatter that will be created will take the timezone from the passed in object. The timezone database bundled with PHP will not be used – ICU's will be used instead. The timezone identifier used in DateTime objects must therefore also exist in ICU's database.
Список параметров
-
object
-
An object of type IntlCalendar or DateTime. The timezone information in the object will be used.
-
format
-
How to format the date/time. This can either be an array with two elements (first the date style, then the time style, these being one of the constants
IntlDateFormatter::NONE
,IntlDateFormatter::SHORT
,IntlDateFormatter::MEDIUM
,IntlDateFormatter::LONG
,IntlDateFormatter::FULL
), a long with the value of one of these constants (in which case it will be used both for the time and the date) or a string with the format described in » the ICU documentation. IfNULL
, the default style will be used. -
locale
-
The locale to use, or
NULL
to use the default one.
Возвращаемые значения
A string with result или FALSE
в случае возникновения ошибки.
Примеры
Пример #1 IntlDateFormatter::formatObject() examples
<?php
/* default timezone is irrelevant; timezone taken from the object */
ini_set('date.timezone', 'UTC');
/* default locale is taken from this ini setting */
ini_set('intl.default_locale', 'fr_FR');
$cal = IntlCalendar::fromDateTime("2013-06-06 17:05:06 Europe/Dublin");
echo "default:\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 "with 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
- PHP Руководство
- Функции по категориям
- Индекс функций
- Справочник функций
- Поддержка языков и кодировок
- Internationalization Functions
- Функция IntlDateFormatter::create() - Create a date formatter
- Функция IntlDateFormatter::format() - Format the date/time value as a string
- Функция IntlDateFormatter::formatObject() - Formats an object
- Функция IntlDateFormatter::getCalendar() - Get the calendar type used for the IntlDateFormatter
- Функция IntlDateFormatter::getDateType() - Get the datetype used for the IntlDateFormatter
- Функция IntlDateFormatter::getErrorCode() - Get the error code from last operation
- Функция IntlDateFormatter::getErrorMessage() - Get the error text from the last operation.
- Функция IntlDateFormatter::getLocale() - Get the locale used by formatter
- Функция IntlDateFormatter::getPattern() - Get the pattern used for the IntlDateFormatter
- Функция IntlDateFormatter::getTimeType() - Get the timetype used for the IntlDateFormatter
- Функция IntlDateFormatter::getTimeZoneId() - Get the timezone-id used for the IntlDateFormatter
- Функция IntlDateFormatter::getCalendarObject() - Get copy of formatterʼs calendar object
- Функция IntlDateFormatter::getTimeZone() - Get formatterʼs timezone
- Функция IntlDateFormatter::isLenient() - Get the lenient used for the IntlDateFormatter
- Функция IntlDateFormatter::localtime() - Parse string to a field-based time value
- Функция IntlDateFormatter::parse() - Parse string to a timestamp value
- Функция IntlDateFormatter::setCalendar() - Sets the calendar type used by the formatter
- Функция IntlDateFormatter::setLenient() - Set the leniency of the parser
- Функция IntlDateFormatter::setPattern() - Set the pattern used for the IntlDateFormatter
- Функция IntlDateFormatter::setTimeZoneId() - Sets the time zone to use
- Функция IntlDateFormatter::setTimeZone() - Sets formatterʼs timezone
Коментарии
`format` vs static `formatObject`
The `formatObject` is slower! `format` is more then 10-13 times faster! (on PHP 5.5) Use the `format` method instead of the static `formatObject`.
php -v
PHP 5.5.26-1+deb.sury.org~precise+1 (cli) (built: Jun 15 2015 10:04:01)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2015 Zend Technologies
with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2015, by Zend Technologies
with 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 s
`format` : 0.033759 s
It's still slower on php7.1, but not that dramatic anymore, here I got something around 5times slower
If you want to format a date according to a specific scheme and in a local language, here is the link to the reference of the formatting codes to use, I did not find it directly in the documentation:
https://unicode-org.github.io/icu/userguide/format_parse/datetime/#date-field-symbol-table
Here is a test using a DateTime object :
<?php
// must be required by the server
date_default_timezone_set( 'Europe/Paris' );
// instantiate a new DateTime object
$dateTimeObj = new DateTime('now', new DateTimeZone('Europe/Paris'));
// format the date with a specific scheme
// the 3 parameters are [ DateTimeObject, ICU Scheme, locale code string ]
$dateFromatted = IntlDateFormatter::formatObject( $dateTimeObj, "eee d MMMM y à HH:mm", 'fr' );
// test :
echo ucwords($dateFromatted);
// output : Jeu. 7 Avril 2022 à 04:36 // formatted as i want
?>
`format` vs static `formatObject`
** The `formatObject` is NOT slower! (on PHP 5.5) **
Use either the `format` method or the static `formatObject`.
Since `formatObject` is doing the work of `new IntlDateFormatter` with the provided pattern, the instantiation line must be included in the loop!
Another debunked flawed false test!
php -v
PHP 5.5.26-1+deb.sury.org~precise+1 (cli) (built: Jun 15 2015 10:04:01)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2015 Zend Technologies
with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2015, by Zend Technologies
with 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.336579s
`format` : 0.391158s