/** * Validates a date value. * * @param string $value The value to validate. * * @return bool|string Returns boolean TRUE upon successfull validation, and an error message string upon failure. */ public function validate(&$value) { $return = parent::validate($value); if ($return !== true) { return $return; } if (isset($value) && strlen(trim($value)) > 0) { $msglbl = get::array_def($this->attributes, 'msglbl', get::array_def($this->attributes, 'name', $this->getId())); $valid = is::date($value); if ($valid === false) { return sprintf('"%s" does not have a valid date. Dates must be in the format YYYY-mm-dd.', $msglbl); } $mode = get::array_def($this->attributes, 'datemode', 'html'); $min = get::array_def($this->attributes, 'min', false); if (is_object($min) && $min instanceof cDateTime) { if ($valid->lessThan($min)) { switch ($mode) { case 'html': break; case 'us': $min = $min->format_us(); break; default: $min = $min->format($mode); break; } return sprintf('"%s" cannot be before "%s".', $msglbl, $min); } } $max = get::array_def($this->attributes, 'max', false); if (is_object($max) && $max instanceof cDateTime) { if ($max->lessThan($valid)) { switch ($mode) { case 'html': break; case 'us': $max = $max->format_us(); break; default: $max = $max->format($mode); break; } return sprintf('"%s" cannot be after "%s".', $msglbl, $max); } } $value = $valid; } return true; }
/** * Checks to see if the given input is a valid date/time according to the HTML5 * specifications used for datetime fields. * * See http://dev.w3.org/html5/spec/common-microsyntaxes.html#dates * for the rules on parsing dates * * @param string $input * The value to check. * * @return mixed * Returns a boolean false if the input is not a time, otherwise it * returns an array with the following keys. * - year: The numeric year. * - month: The numeric month. * - day: The numeric day. * - formatted: US formatted date string m/d/y. * - hour: The numeric hour in 24 notation. * - minute: The numeric minute. * - second: The numeric second. * - faction: The numeric fraction of a second. * - meridiem: AM/PM * - format24: The formatted time using 24 hour notation. * - format12: The formatted time using 12 hour notation without the meridiem. * - unixdatetime: The unix datetimestamp as created by mktime. * - datetime: A DateTime class containing the date/time. * - isutc: Boolean indicating whether the time is UTC(Zulu) time or not. */ public static function datetime($input, $local = false) { if (!isset($input)) { return false; } $in = trim($input); if (substr_count($in, 'T') != 1) { return false; } list($datepart, $timepart) = explode('T', $in); $dateValid = is::date($datepart); if ($dateValid === false) { return false; } $timeValid = is::time($timepart); if ($timeValid === false) { return false; } $timeValid->setDate($dateValid->format('Y'), $dateValid->format('m'), $dateValid->format('d')); return new cDateTime($timeValid, $local === true ? cDateTime::DATETIMELOCAL_KIND : cDateTime::DATETIME_KIND); }