Hello folks! welcome back to a new edition of our tutorial on PHP. In this tutorial guide, we are going to be studying about the PHP date_get_last_errors() Function.
The built-in date_get_last_errors() function in PHP is an alias of the built-in PHP DateTime::getLastErrors()::_construct(). This function is used in getting the warnings and errors that occurred while parsing a date string.
The built-in date_get_last_errors() function in PHP is an alias of the built-in PHP 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 built-in PHP function does not accept any parameters.
Return Value
This function returns an array that contains all warnings and errors which 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 displays the warnings and errors that occurred while creating a DateTime object using the built-in PHP 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 going to be rounding up for this tutorial post. In our next tutorial, we will be discussing about the date_interval_create_from_date_string() Function in PHP.
Do feel free to ask your questions where necessary and we 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.
Do feel free to ask your questions where necessary and we 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.