1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
| <?php
/**
* Runs a regular expression match.
*
* @param string $check Value to check against the $regex expression
* @param string $regex Regular expression
* @return bool Success of match
*/
function _check($check, $regex) {
if (is_string($regex) && preg_match($regex, $check)) {
return true;
}
return false;
}
/**
* Date validation, determines if the string passed is a valid date.
* keys that expect full month, day and year will validate leap years
*
* ### Formats:
*
* - `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 or 12/06 separators can be a space, period, dash, forward slash
* - `ym` 2006/12 or 06/12 separators can be a space, period, dash, forward slash
* - `y` 2006 just the year without any separators
*
* @param string $check a valid date string
* @param string|array $format Use a string or an array of the keys above.
* Arrays should be passed as array('dmy', 'mdy', etc)
* @param string $regex If a custom regular expression is used this is the only validation that will occur.
* @return bool Success
*/
function date($check, $format = 'ymd', $regex = null) {
if ($regex !== null) {
return check($check, $regex);
}
$month = '(0[123456789]|10|11|12)';
$separator = '([- /.])';
$fourDigitYear = '(([1][9][0-9][0-9])|([2][0-9][0-9][0-9]))';
$twoDigitYear = '([0-9]{2})';
$year = '(?:' . $fourDigitYear . '|' . $twoDigitYear . ')';
$regex['dmy'] = '%^(?:(?:31(\\/|-|\\.|\\x20)(?:0?[13578]|1[02]))\\1|(?:(?:29|30)' .
$separator . '(?:0?[1,3-9]|1[0-2])\\2))(?:(?:1[6-9]|[2-9]\\d)?\\d{2})$|^(?:29' .
$separator . '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])' .
$separator . '(?:(?: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])' .
$separator . '(?:29|30)\\2))(?:(?:1[6-9]|[2-9]\\d)?\\d{2})$|^(?:0?2' . $separator . '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]))' .
$separator . '(?: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)))' .
$separator . '(?:0?2\\1(?:29)))|(?:(?:(?:1[6-9]|[2-9]\\d)?\\d{2})' .
$separator . '(?:(?:(?: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)?|(Sep)(tember)?|(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)?)' .
$separator . '((1[6-9]|[2-9]\\d)\\d{2})$%';
$regex['my'] = '%^(' . $month . $separator . $year . ')$%';
$regex['ym'] = '%^(' . $year . $separator . $month . ')$%';
$regex['y'] = '%^(' . $fourDigitYear . ')$%';
$format = (is_array($format)) ? array_values($format) : array($format);
foreach ($format as $key) {
if (check($check, $regex[$key]) === true) {
return true;
}
}
return false;
} |
Partager