English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP date_get_last_errors() Function Usage and Examples

PHP Date & Time Function Manual

The date_get_last_errors() function retrieves warning and error information

Definition and Usage

date_get_last_errors() is an alias of DateTime::getLastErrors()::__construct(). This function is used to get the warnings and errors that occur when analyzing date strings.

Syntax

date_get_last_errors();

Parameters

This function does not accept any parameters

Return value

The PHP date_get_last_errors() function returns an array containing all the warnings and errors that occurred when you tried to parse a date string.

PHP version

This function was initially introduced in PHP version5.5introduced in version 5.3.0 and can be used in all higher versions.

Online example

The following example demonstratesdate_get_last_errors()Function usage-

<?php
   date_create("215-7896-848");
   $errors = date_get_last_errors();
   print_r($errors);
?>
Test and see‹/›

Output result

Array
(
    [warning_count] => 1
    [warnings] => Array
        (
            [8=> Double timezone specification
        )
    [error_count] => 5
    [errors] => Array
        (
            [0] => Unexpected character
            [1=> Unexpected character
            [2=> Unexpected character
            [6=> Unexpected character
            [7=> Unexpected character
        )
)

Online example

With this function, you can capture errors that occur when creating a date, as shown below-

<?php
   try { 
      $res = new DateTime("215-7896-848");
      print($res);
   } catch (Exception $e) { 
      print_r(DateTime::getLastErrors()); 
   }  
?>
Test and see‹/›

Output result

Array
(
    [warning_count] => 1
    [warnings] => Array
        (
            [8=> Double timezone specification
        )
    [error_count] => 5
    [errors] => Array
        (
            [0] => Unexpected character
            [1=> Unexpected character
            [2=> Unexpected character
            [6=> Unexpected character
            [7=> Unexpected character
        )
)

Online example

The following example shows the use ofdate_create_from_format()Errors occurred when creating a DateTime object/Warning-

//Create a DateTime object
$date = "25-Mar-1989";
$format = "d-Z-Y";
$res = date_create_from_format($format, $date);
print_r(date_get_last_errors());
Test and see‹/›

Output result

Array
(
    [warning_count] => 0
    [warnings] => Array
        (
        )
    [error_count] => 3
    [errors] => Array
        (
            [3=> The format separator does not match
            [4=> Unexpected data found.
        )
)