IntlDateFormatter::formatObject

datefmt_format_object

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

IntlDateFormatter::formatObject -- datefmt_format_objectFormatea un objeto

Descripción

Estilo orientado a objetos

public static IntlDateFormatter::formatObject(object $object, mixed $format = NULL, string $locale = NULL): string

Estilo por procedimientos

public static datefmt_format_object(object $object, mixed $format = NULL, string $locale = NULL): string

Esta función permite formatear un objeto IntlCalendar o DateTime sin crear primero explícitamente un objeto IntlDateFormatter.

El objeto IntlDateFormatter temporal que se creará tomará la zona horaria del objeto pasado. La base de datos de zonas horarias incluida con PHP no se usara; se usará en su lugar la de ICU. El identificador de zona horaria usado en objetos DateTime debe, por lo tanto, existir también en la base de datos de ICU.

Parámetros

object

Un objeto de tipo IntlCalendar o DateTime. Se usrará la información de la zona horaria del objeto.

format

Cómo formatear la fecha/hora. Puede ser un array con dos elementos (el primero el estilo de fecha, y luego el estilo de la hora, siendo ambos una de las constantes IntlDateFormatter::NONE, IntlDateFormatter::SHORT, IntlDateFormatter::MEDIUM, IntlDateFormatter::LONG, IntlDateFormatter::FULL), un long con el valor de una de estas constantes (en cuyo caso se usará tanto para la hora como para la fecha) o un string con el formato descrito en » la documentación de ICU. Si es null, se utilizará el estilo predeterminado.

locale

La configuración regional a usar, o null para usar la predeterminada.

Valores devueltos

Un string con resultados o false en caso de error.

Ejemplos

Ejemplo #1 Ejemplos de IntlDateFormatter::formatObject()

<?php
/* la zona horaria predeterminada es irrelevante; se toma del objeto */
ini_set('date.timezone', 'UTC');
/* la configuración regional predeterminada se toma de este ajuste ini */
ini_set('intl.default_locale', 'fr_FR');

$cal = IntlCalendar::fromDateTime("2013-06-06 17:05:06 Europe/Dublin");
echo
"predeterminado:\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
"con DateTime:\n\t",
IntlDateFormatter::formatObject(
new
DateTime("2013-09-09 09:09:09 Europe/Madrid"),
IntlDateFormatter::FULL,
'es_ES'),
"\n";

El resultado del ejemplo sería:

predeterminado:
    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
con DateTime:
    lunes, 9 de septiembre de 2013 09:09:09 Hora de verano de Europa central

add a note add a note

User Contributed Notes 2 notes

up
1
ferenczi dot krisztian at gmail dot com
8 years ago
`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
up
-1
sebastian at huehnerhose dot de
6 years ago
It's still slower on php7.1, but not that dramatic anymore, here I got something around 5times slower
To Top