In PHP, date_get_last_errors() returns the warnings and errors from the last date or time string that PHP parsed.
It is the procedural name for DateTimeImmutable::getLastErrors() (and DateTime::getLastErrors()). Call it right after date_create(), date_create_from_format(), or similar functions to find out why a date looked wrong.
Syntax
Call date_get_last_errors() with the values you need. Start with this line.
PHP
$errors = date_get_last_errors();
Call
What it does
date_get_last_errors()
Returns an array with warning_count, warnings, error_count, and errors.
$errors["warning_count"]
Number of warnings, such as a date like 2024-02-30 that rolled over.
$errors["errors"]
An array of messages, indexed by the position in the string where the problem was found.
The result describes only the most recent parse, so call it straight after the function you want to check. In PHP 8.2 and later it returns false when there were no warnings or errors. In older versions it returns an array with zero counts. In new code you can also use DateTimeImmutable::getLastErrors().
Library declarations
PHP
date_get_last_errors(): array|false
Parameters
Parameter
Description
—
This function takes no parameters.
Return Value
Return Value
Description
array
An array with the keys warning_count, warnings, error_count, and errors.
false
Returned in PHP 8.2 and later when the last parse had no warnings or errors.
PHP Examples
Example 1 — Read Warnings From date_create()
The date 2024-02-30 does not exist. PHP still creates a date, but it records a warning.
The format Y-m-d expects exactly a date, but the text has extra words after it.
date_create_from_format() returns false, which var_dump() prints as bool(false).
date_get_last_errors() reports one error.
reset() gives the first message in the errors array, which is Trailing data.
Frequently Asked Questions
It returns details about warnings and errors from the last date or time string that PHP parsed. It is useful for finding out why a date was rejected or changed.
In PHP 8.2 and later it returns false when the last parse had no warnings or errors. In earlier versions it always returned an array. Check for false before you read array keys.
A warning means PHP built a date but adjusted something, like rolling 2024-02-30 to March 1. An error means PHP could not parse the text, so functions like date_create_from_format() return false.
Yes. It is the procedural alias for DateTime::getLastErrors() and DateTimeImmutable::getLastErrors(). They all return the same information.
Call it immediately after the parsing function you want to check. Any later parse replaces the stored result, so waiting can give you the wrong answer.