PHP | date_get_last_errors() Function

PHP date_get_last_errors() Function


Hello dear readers! welcome back to another edition of our tutorial on PHP. In this tutorial guide, we are going to be discussing about the date_get_last_errors() Function in PHP.

The built-in date_get_last_errors() function in PHP is an alias of the DateTime::getLastErrors()::_construct(). This function is used in getting the warnings and errors that occurred while parsing a date string.

Syntax

Following below is the syntax to use this function -

date_get_last_errors();


Parameter Details

This function does not accept any parameters.

Return Value

This PHP function returns an array that contains all the warnings and errors that occurs when you try to parse a date string.

PHP Version

This function was first introduced as part of core PHP version 5.5.0 and, it works with all of the later versions.

Example1

Following example demonstrates the usage of date_get_last_errors() function -

<?php
   date_create("215-7896-848");
   $errors = date_get_last_errors();
   print_r($errors);
?>

Output

When the above code is executed, it will produce the following 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
        )

)

Example2

Using this function you can catch the errors occurred while creating a date as shown below -

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

Output

When the above code is executed, it will produce the following 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
        )

)

Example3

Following example below displays the warnings/errors that occurred while creating a DateTime object using date_create_from_format() function -

//Creating a DateTime object
$date = "25-Mar-1989";
$format = "d-Z-Y";
$res = date_create_from_format($format, $date);
print_r(date_get_last_errors());

Output

When the above code is executed, it will produce the following result -

Array
(
    [warning_count] => 0
    [warnings] => Array
        (
        )

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

)


Alright guys! This is where we are rounding up for this tutorial post. In our next tutorial guide, we are going to be discussing about the date_interval_create_from_date_string() Function in PHP.

Do feel free to ask your questions where necessary and i will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.