You are here

function date_is_valid in Date 6

Same name and namespace in other branches
  1. 5.2 date_api.module \date_is_valid()
  2. 5 date.inc \date_is_valid()
  3. 6.2 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

9 calls to date_is_valid()
DateAPI::testDateAPI in tests/date_api.test
date_convert_from_custom in ./date_api_elements.inc
Convert a date input in a custom format to a standard date type
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_php4.inc in date_php4/date_php4.inc

... See full list

File

./date_api.module, line 1076
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) {
  if (empty($date)) {
    return FALSE;
  }
  if ($type == DATE_OBJECT && !is_object($date)) {
    return FALSE;
  }
  if (($type == DATE_ISO || $type == DATE_DATETIME) && !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;
  }

  // If checkdate works, no need for further tests.
  $year = date_part_extract($date, 'year', $type);
  $month = date_part_extract($date, 'month', $type);
  $day = date_part_extract($date, 'day', $type);
  if (!checkdate($month, $day, $year)) {

    // ISO dates and arrays can have empty date parts
    if ($type == DATE_ISO || $type == DATE_ARRAY) {
      if (variable_get('date_max_year', 4000) < $year || variable_get('date_min_year', 1) > $year || 12 < $month || 0 > $month || 31 < $day || 0 > $day) {
        return FALSE;
      }
    }
    elseif (variable_get('date_max_year', 4000) < $year || variable_get('date_min_year', 1) > $year || 12 < $month || 1 > $month || 31 < $day || 1 > $day) {
      return FALSE;
    }
  }
  return TRUE;
}