DateTimeImmutable::getLastErrors
(PHP 5 >= 5.5.0, PHP 7, PHP 8)
DateTimeImmutable::getLastErrors — Devuelve las advertencias y errores
Descripción
public static DateTimeImmutable::getLastErrors():
array|false
Parámetros
Esta función no contiene ningún parámetro.
Valores devueltos
Devuelve un array que contiene información sobre advertencias y errores, o false si no
hay ni advertencias ni errores.
Ejemplos
Ejemplo #1 DateTimeImmutable::getLastErrors() example
<?php
try {
$date = new DateTimeImmutable('asdfasdf');
} catch (Exception $e) {
// Solo para fines de demostración...
print_r(DateTimeImmutable::getLastErrors());
// La forma correcta de hacer esto con programación orientada a objetos es
echo $e->getMessage();
}
?>
El ejemplo anterior mostrará:
Array
(
[warning_count] => 1
[warnings] => Array
(
[6] => Double timezone specification
)
[error_count] => 1
[errors] => Array
(
[0] => The timezone could not be found in the database
)
)
Failed to parse time string (asdfasdf) at position 0 (a): The timezone could not be found in the database
Los índices 6, y 0 en la salida de ejemplo se refieren al índice de caracteres en la cadena donde ocurrió el error.
Ejemplo #2 Detectando fechas desbordadas
<?php
$date = DateTimeImmutable::createFromFormat('!Y-m-d', '2020-02-30');
print_r(DateTimeImmutable::getLastErrors());
El ejemplo anterior mostrará:
Array
(
[warning_count] => 1
[warnings] => Array
(
[10] => The parsed date was invalid
)
[error_count] => 0
[errors] => Array
(
)
)