note 65291 added to function.checkdate
| From: | rainjam at gmail dot com | Date: | Fri, 28 Apr 2006 10:32:59 +0000 |
| Subject: | note 65291 added to function.checkdate | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-109947@lists.php.net to get a copy of this message | ||
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
----
Server IP: 66.163.161.117
Probable Submitter: 213.218.225.25
----
X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER
autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.checkdate.php
Edit -- http://master.php.net/note/edit/65291
Del: integrated -- http://master.php.net/note/delete/65291/integrated
Del: useless -- http://master.php.net/note/delete/65291/useless
Del: bad code -- http://master.php.net/note/delete/65291/bad+code
Del: spam -- http://master.php.net/note/delete/65291/spam
Del: non-english -- http://master.php.net/note/delete/65291/non-english
Del: in docs -- http://master.php.net/note/delete/65291/in+docs
Del: other reasons-- http://master.php.net/note/delete/65291
Reject -- http://master.php.net/note/reject/65291
Search -- http://master.php.net/manage/user-notes.php