/**
  * Date validation, determines if the string passed is a valid date.
  * keys that expect full month, day and year will validate leap years
  *
  * @param string $check a valid date string
  * @param mixed $format Use a string or an array of the keys below. Arrays should be passed as array('dmy', 'mdy', etc)
  * 					Keys: dmy 27-12-2006 or 27-12-06 separators can be a space, period, dash, forward slash
  * 							mdy 12-27-2006 or 12-27-06 separators can be a space, period, dash, forward slash
  * 							ymd 2006-12-27 or 06-12-27 separators can be a space, period, dash, forward slash
  * 							dMy 27 December 2006 or 27 Dec 2006
  * 							Mdy December 27, 2006 or Dec 27, 2006 comma is optional
  * 							My December 2006 or Dec 2006
  * 							my 12/2006 separators can be a space, period, dash, forward slash
  * @param string $regex If a custom regular expression is used this is the only validation that will occur.
  * @return boolean Success
  * @access public
  */
 function date($check, $format = 'ymd', $regex = null)
 {
     if (is_numeric($check)) {
         return WPToolset_Field_Date_Scripts::_isTimestampInRange($check);
     }
     // TODO Change to use date strtotime
     $valid = wptoolset_strtotime($check);
     return $valid !== false;
     $cake_date_formats = array('F j, Y' => 'Mdy', 'Y/m/d' => 'ymd', 'm/d/Y' => 'mdy', 'd/m/Y' => 'dmy');
     $format = isset($cake_date_formats[$format]) ? $cake_date_formats[$format] : $format;
     $_this =& WPToolset_Cake_Validation::getInstance();
     $_this->__reset();
     $_this->check = $check;
     $_this->regex = $regex;
     if (!is_null($_this->regex)) {
         return $_this->_check();
     }
     $regex['dmy'] = '%^(?:(?:31(\\/|-|\\.|\\x20)(?:0?[13578]|1[02]))\\1|(?:(?:29|30)(\\/|-|\\.|\\x20)(?:0?[1,3-9]|1[0-2])\\2))(?:(?:1[6-9]|[2-9]\\d)?\\d{2})$|^(?:29(\\/|-|\\.|\\x20)0?2\\3(?:(?:(?:1[6-9]|[2-9]\\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\\d|2[0-8])(\\/|-|\\.|\\x20)(?:(?:0?[1-9])|(?:1[0-2]))\\4(?:(?:1[6-9]|[2-9]\\d)?\\d{2})$%';
     $regex['mdy'] = '%^(?:(?:(?:0?[13578]|1[02])(\\/|-|\\.|\\x20)31)\\1|(?:(?:0?[13-9]|1[0-2])(\\/|-|\\.|\\x20)(?:29|30)\\2))(?:(?:1[6-9]|[2-9]\\d)?\\d{2})$|^(?:0?2(\\/|-|\\.|\\x20)29\\3(?:(?:(?:1[6-9]|[2-9]\\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:(?:0?[1-9])|(?:1[0-2]))(\\/|-|\\.|\\x20)(?:0?[1-9]|1\\d|2[0-8])\\4(?:(?:1[6-9]|[2-9]\\d)?\\d{2})$%';
     $regex['ymd'] = '%^(?:(?:(?:(?:(?:1[6-9]|[2-9]\\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00)))(\\/|-|\\.|\\x20)(?:0?2\\1(?:29)))|(?:(?:(?:1[6-9]|[2-9]\\d)?\\d{2})(\\/|-|\\.|\\x20)(?:(?:(?:0?[13578]|1[02])\\2(?:31))|(?:(?:0?[1,3-9]|1[0-2])\\2(29|30))|(?:(?:0?[1-9])|(?:1[0-2]))\\2(?:0?[1-9]|1\\d|2[0-8]))))$%';
     $regex['dMy'] = '/^((31(?!\\ (Feb(ruary)?|Apr(il)?|June?|(Sep(?=\\b|t)t?|Nov)(ember)?)))|((30|29)(?!\\ Feb(ruary)?))|(29(?=\\ Feb(ruary)?\\ (((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)))))|(0?[1-9])|1\\d|2[0-8])\\ (Jan(uary)?|Feb(ruary)?|Ma(r(ch)?|y)|Apr(il)?|Ju((ly?)|(ne?))|Aug(ust)?|Oct(ober)?|(Sep(?=\\b|t)t?|Nov|Dec)(ember)?)\\ ((1[6-9]|[2-9]\\d)\\d{2})$/';
     $regex['Mdy'] = '/^(?:(((Jan(uary)?|Ma(r(ch)?|y)|Jul(y)?|Aug(ust)?|Oct(ober)?|Dec(ember)?)\\ 31)|((Jan(uary)?|Ma(r(ch)?|y)|Apr(il)?|Ju((ly?)|(ne?))|Aug(ust)?|Oct(ober)?|(Sept|Nov|Dec)(ember)?)\\ (0?[1-9]|([12]\\d)|30))|(Feb(ruary)?\\ (0?[1-9]|1\\d|2[0-8]|(29(?=,?\\ ((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)))))))\\,?\\ ((1[6-9]|[2-9]\\d)\\d{2}))$/';
     $regex['My'] = '%^(Jan(uary)?|Feb(ruary)?|Ma(r(ch)?|y)|Apr(il)?|Ju((ly?)|(ne?))|Aug(ust)?|Oct(ober)?|(Sep(?=\\b|t)t?|Nov|Dec)(ember)?)[ /]((1[6-9]|[2-9]\\d)\\d{2})$%';
     $regex['my'] = '%^(((0[123456789]|10|11|12)([- /.])(([1][9][0-9][0-9])|([2][0-9][0-9][0-9]))))$%';
     $format = is_array($format) ? array_values($format) : array($format);
     foreach ($format as $key) {
         $_this->regex = $regex[$key];
         if ($_this->_check() === true) {
             return true;
         }
     }
     return false;
 }
Exemple #2
0
/**
 * Checks if timestamp is numeric and within range.
 * 
 * @param type $timestamp
 * @return type
 */
function wpcf_fields_date_timestamp_is_valid($timestamp)
{
    /*
     * http://php.net/manual/en/function.strtotime.php
     * The valid range of a timestamp is typically
     * from Fri, 13 Dec 1901 20:45:54 UTC
     * to Tue, 19 Jan 2038 03:14:07 UTC.
     * (These are the dates that correspond to the minimum
     * and maximum values for a 32-bit signed integer.)
     * Additionally, not all platforms support negative timestamps,
     * therefore your date range may be limited to no earlier than
     * the Unix epoch.
     * This means that e.g. dates prior to Jan 1, 1970 will not
     * work on Windows, some Linux distributions,
     * and a few other operating systems.
     * PHP 5.1.0 and newer versions overcome this limitation though. 
     */
    // MIN 'Jan 1, 1970' - 0 | Fri, 13 Dec 1901 20:45:54 UTC
    $_min_timestamp = fields_date_timestamp_neg_supported() ? -2147483646 : 0;
    // MAX 'Tue, 19 Jan 2038 03:14:07 UTC' - 2147483647
    $_max_timestamp = 2147483647;
    return WPToolset_Field_Date_Scripts::_isTimestampInRange($timestamp);
    //return is_numeric( $timestamp ) && $_min_timestamp <= $timestamp && $timestamp <= $_max_timestamp;
}
 protected static function _isTimestampInRange($timestamp)
 {
     return WPToolset_Field_Date_Scripts::_isTimestampInRange($timestamp);
 }