note 65291 deleted from function.checkdate by bjori
| From: | bjori@php.net | Date: | Fri, 28 Apr 2006 13:16:20 +0000 |
| Subject: | note 65291 deleted from function.checkdate by bjori | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-109976@lists.php.net to get a copy of this message | ||
Note Submitter: rainjam at gmail dot com
----
Slightly modified so you don't have to specify a format string. Returns false if the
date's invalid; this checks for dd/mm/yyyy first, but to check for eg. mm/dd/yyyy just swap
round the $forms array.
function datecheck($date, $yearepsilon=5000) {
$forms = array('dmy', 'mdy', 'ydm', 'dym',
'myd', 'ymd');
foreach ($forms as $form) {
$date=str_replace("/", "-", $date);
$form = strtolower($form);
if (count($datebits=explode('-',$date))!=3) return false;
$year = intval($datebits[strpos($form, 'y')]);
$month = intval($datebits[strpos($form, 'm')]);
$day = intval($datebits[strpos($form, 'd')]);
if ((abs($year-date('Y'))>$yearepsilon) || // year outside given range
($month<1) || ($month>12) || ($day<1) ||
(($month==2) && ($day>28+(!($year%4))-(!($year%100))+(!($year%400)))) ||
($day>30+(($month>7)^($month&1)))) {
// date out of range, so check next format
} else {
// date is ok, so return it
return array(
'year' => $year,
'month' => $month,
'day' => $day
);
}
}
// no date found so...
return false;
}
Nick