note 56024 deleted from function.checkdate by nlopess

From: Date: Wed, 26 Jul 2006 12:51:37 +0000
Subject: note 56024 deleted from function.checkdate by nlopess
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-115210@lists.php.net to get a copy of this message
Note Submitter: </Life>.org ---- The function of phil@philmo contains a few errors... I corrected them as far as I found them and tried to improve the code. <?php /** * Checks for a valid date * * @param string Date in the format "MM-DD-YYYY" * @param integer Disallow years more than $yearepsilon from now (in future as well as in past) * @return array [ month, day, year ] * @since 1.0 */ /* fixed: ** - leap years ** - december now is allowed ** - removed unnecessary checking ** if you find any more mistakes, please correct them. :) */ function datecheck($date, $yearepsilon=5000) { if (count($datebits=explode('-',$date))!=3) return false; $month = intval($datebits[0]); $day = intval($datebits[1]); $year = intval($datebits[2]); 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)))) return false; // date out of range return array( 'year' => $year, 'month' => $month, 'day' => $day ); } ?>

« previous php.notes (#115210) next »