Checks that arrays of date parts will create a valid date.
Checks that an array of date parts has a year, month, and day, and that those values create a valid date. If time is provided, verifies that the time values are valid. Sort of an equivalent to checkdate().
array $array: An array of datetime values keyed by date part.
boolean TRUE if the datetime parts contain valid values, otherwise FALSE.
public static function checkArray($array) {
$valid_date = FALSE;
$valid_time = TRUE;
// Check for a valid date using checkdate(). Only values that
// meet that test are valid.
if (array_key_exists('year', $array) && array_key_exists('month', $array) && array_key_exists('day', $array)) {
if (@checkdate($array['month'], $array['day'], $array['year'])) {
$valid_date = TRUE;
}
}
// Testing for valid time is reversed. Missing time is OK,
// but incorrect values are not.
foreach (array(
'hour',
'minute',
'second',
) as $key) {
if (array_key_exists($key, $array)) {
$value = $array[$key];
switch ($value) {
case 'hour':
if (!preg_match('/^([1-2][0-3]|[01]?[0-9])$/', $value)) {
$valid_time = FALSE;
}
break;
case 'minute':
case 'second':
default:
if (!preg_match('/^([0-5][0-9]|[0-9])$/', $value)) {
$valid_time = FALSE;
}
break;
}
}
}
return $valid_date && $valid_time;
}