PHP date_get_last_errors() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Date/Time

Definition

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();
CallWhat 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

ParameterDescription
—This function takes no parameters.

Return Value

Return ValueDescription
arrayAn array with the keys warning_count, warnings, error_count, and errors.
falseReturned 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.

PHP
<?php
date_default_timezone_set("UTC");

$date = date_create("2024-02-30");
$errors = date_get_last_errors();

echo "Date created: ", date_format($date, "Y-m-d"), PHP_EOL;
echo "Warnings: ", $errors["warning_count"], PHP_EOL;
echo "Errors: ", $errors["error_count"], PHP_EOL;

How It Works

  1. Set the timezone. This keeps the example the same on every server.
  2. date_create("2024-02-30") works, but February 2024 has only 29 days, so PHP moves the date to 2024-03-01.
  3. date_get_last_errors() returns the details of that parse. It found one warning.
  4. The warning count is 1 and the error count is 0, because the date could still be built.

Example 2 — No Problems Found

For a valid date, newer PHP versions return false and older versions return zero counts. This code handles both.

PHP
<?php
date_default_timezone_set("UTC");

$date = date_create("2024-03-15");
$errors = date_get_last_errors();

if ($errors === false || ($errors["warning_count"] === 0 && $errors["error_count"] === 0)) {
    echo "No problems found", PHP_EOL;
} else {
    echo "Something was wrong", PHP_EOL;
}

How It Works

  1. The date 2024-03-15 is valid, so the parse is clean.
  2. In PHP 8. 2 and later, date_get_last_errors() returns false here.
  3. In older versions it returns an array where both counts are 0.
  4. The condition checks for both cases, so the message is the same everywhere.

Example 3 — Show the Error Message

When date_create_from_format() cannot match the text, it returns false. The error list tells you why.

PHP
<?php
$date = date_create_from_format("Y-m-d", "2024-03-15 extra");
var_dump($date);

$errors = date_get_last_errors();
echo "Error count: ", $errors["error_count"], PHP_EOL;
echo reset($errors["errors"]), PHP_EOL;

How It Works

  1. The format Y-m-d expects exactly a date, but the text has extra words after it.
  2. date_create_from_format() returns false, which var_dump() prints as bool(false).
  3. date_get_last_errors() reports one error.
  4. 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.

Next: date_interval_create_from_date_string()

date_interval_create_from_date_string() builds a DateInterval object from a plain-English .

date_interval_create_from_date_string() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful