$date = new DateTime('2021-10-01 12:00:00'); $result = $date->modify('invalid time'); $lastErrors = $date->getLastErrors(); print_r($lastErrors);
Array ( [warning_count] => 0 [warnings] => Array ( ) [error_count] => 1 [errors] => Array ( [15] => Data missing ) [is_localtime] => )
$date = new DateTime('2021-02-31'); $lastErrors = $date->getLastErrors(); print_r($lastErrors);
Array ( [warning_count] => 1 [warnings] => Array ( [4] => The parsed date was invalid ) [error_count] => 0 [errors] => Array ( ) [is_localtime] => )This example demonstrates how the getLastErrors method can be used to retrieve information about a warning that occurred during the creation of a DateTime object. In this case, the warning message is "The parsed date was invalid". The DateTime class is part of the PHP core library.