PHP Function Reference

PHP DateTime - getLastErrors() Method



The PHP DateTime::getLastErrors() method returns an array of warnings and errors found while parsing a date/time string. The date_get_last_errors() function is an alias of this method.

Syntax

//Object-oriented style
public DateTime::getLastErrors()

//Procedural style
date_get_last_errors()

Parameters

No parameter is required.

Return Value

Returns an array containing info about warnings and errors, or false if there are neither warnings nor errors.

Example: using Object-oriented style

Consider the example below where this method is used in Object-oriented style.

<?php
  try {
    //trying to parsing a invalid date/time string
    $date = new DateTime('103-105-20889');
  } catch (Exception $e) {
    print_r(DateTime::getLastErrors());
  }
?>

The output of the above code will be:

Array
(
    [warning_count] => 1
    [warnings] => Array
        (
            [7] => Double timezone specification
        )

    [error_count] => 5
    [errors] => Array
        (
            [0] => Unexpected character
            [1] => Unexpected character
            [2] => Unexpected character
            [11] => Unexpected character
            [12] => Unexpected character
        )

)

Example: using Procedural style

Consider the example below where this method is used in Procedural style.

<?php
  //trying to parsing a invalid date/time string
  $date = date_create('ABC-DEF-GHIJ');
  print_r(date_get_last_errors());
?>

The output of the above code will be:

Array
(
    [warning_count] => 1
    [warnings] => Array
        (
            [4] => Double timezone specification
        )

    [error_count] => 4
    [errors] => Array
        (
            [0] => The timezone could not be found in the database
            [3] => Unexpected character
            [7] => Unexpected character
            [8] => Double timezone specification
        )

)

❮ PHP Date and Time Reference