function date_is_valid in Date 5.2
Same name and namespace in other branches
- 5 date.inc \date_is_valid()
- 6.2 date_api.module \date_is_valid()
- 6 date_api.module \date_is_valid()
Functions to test the validity of a date in various formats. Has special case for ISO dates and arrays which can be missing month and day and still be valid.
Parameters
$type: could be DATE_ARRAY, DATE_UNIX, DATE_DATETIME, DATE_ISO, or DATE_OBJECT @param $granularity The granularity of the date value provided. Set this for partial dates so they pass validation.
8 calls to date_is_valid()
- DateAPI::testDateAPI in tests/
date_api.test - date_limit_value in ./
date_api.module - Recalculate a date so it only includes elements from a granularity array. Helps prevent errors when unwanted values round up and ensures that unwanted date part values don't get stored in the database.
- date_make_date in ./
date_api.module - Convert a date of any type or an array of date parts into a valid date object.
- date_popup_input_value in date_popup/
date_popup.module - Helper function for extracting a date value out of user input.
- date_select_input_value in ./
date_api_elements.inc - Helper function for extracting a date value out of user input.
File
- ./
date_api.module, line 1198 - This module will make the date API available to other modules. Designed to provide a light but flexible assortment of functions and constants, with more functionality in additional files that are not loaded unless other modules specifically include them.
Code
function date_is_valid($date, $type = DATE_DATETIME, $granularity = array(
'year',
'month',
'day',
'hour',
'minute',
)) {
// Check that the value is properly structured.
if (empty($date)) {
return FALSE;
}
if ($type == DATE_OBJECT && !is_object($date)) {
return FALSE;
}
if (($type == DATE_ISO || $type == DATE_DATETIME) && (!is_string($date) || !preg_match(DATE_REGEX_LOOSE, $date))) {
return FALSE;
}
if ($type == DATE_UNIX and !is_numeric($date)) {
return FALSE;
}
if ($type == DATE_ARRAY and !is_array($date)) {
return FALSE;
}
// Make sure integer values are sent to checkdate.
$year = intval(date_part_extract($date, 'year', $type));
$month = intval(date_part_extract($date, 'month', $type));
$day = intval(date_part_extract($date, 'day', $type));
if (checkdate($month, $day, $year)) {
return TRUE;
}
// If this is an incomplete date (year only or year and month only),
// need special handling, partial dates can have empty date parts.
$max_granularity = $granularity;
$max_granularity = array_pop($max_granularity);
if (!in_array($max_granularity, array(
'year',
'month',
))) {
if (in_array('year', $granularity) && !date_valid_year($year)) {
return FALSE;
}
elseif (in_array('month', $granularity) && !date_valid_month($month)) {
return FALSE;
}
elseif (in_array('day', $granularity) && !date_valid_day($day, $month, $year)) {
return FALSE;
}
}
elseif ($type == DATE_ISO || $type == DATE_ARRAY) {
if (!date_valid_year($year)) {
return FALSE;
}
elseif (!empty($month) && !date_valid_month($month)) {
return FALSE;
}
elseif (!empty($day) && !date_valid_day($day, $month, $year)) {
return FALSE;
}
}
elseif (!date_valid_year($year) || !date_valid_month($month) || date_valid_day($day, $month, $year)) {
// Unix and datetime are expected to have at least a year, month, and day.
return FALSE;
}
return TRUE;
}