It’s important to read the documentation, and then read it again once or twice.
PHP’s date_parse function parses a datetime string and returns an “array with information about the parsed date on success or FALSE on failure.”
This makes it tempting to use the function something like:
$datetime = date_parse($datetime_string); if ( $datetime === FALSE ) { # Failure - deal with the bad $datetime_string } else { # Success - use the $datetime }
However in this case failure means the the date_parse function has failed to work at a fairly fundamental level and reading the docs further you find “In case the date format has an error, the element ‘errors’ will contains the error messages.”
And indeed:
<?php print_r(date_parse('real soon now')); Array( [year] => [month] => [day] => [hour] => [minute] => [second] => [fraction] => [warning_count] => 1 [warnings] => Array( [5] => Double timezone specification ) [error_count] => 1 [errors] => Array( [0] => The timezone could not be found in the database ) [is_localtime] => 1 [zone_type] => 0 )
So in fact what is needed is something more like:
$datetime = date_parse($datetime_string); if ( $datetime === FALSE ) { # Failure - report fatal error processing $datetime_string } else if ( $datetime['error_count'] > 0 ) { # Failure - report errors in $datetime['errors'] } else { # Success - use the $datetime # (but maybe have a look at $datetime['warning_count'] too) }