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(
=>
=>
=>
=>
=>
=>
=>
=> 1
=> Array(
=> Double timezone specification
)
=> 1
=> Array(
=> The timezone could not be found in the database
)
=> 1
=> 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)
}