note 62611 deleted from function.checkdate by nlopess

From: Date: Wed, 26 Jul 2006 12:51:46 +0000
Subject: note 62611 deleted from function.checkdate by nlopess
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-115212@lists.php.net to get a copy of this message
Note Submitter: </Life>.org ---- Taking the comments of Hightechguy into account, here is a version with variable date formatting: <?php /** * Checks for a valid date * * @param string Date in the format given by the format parameter. * @param integer Disallow years more than $yearepsilon from now (in future as well as in past) * @param string Formatting string. Has to be one of 'dmy', 'dym', 'mdy', 'myd', 'ydm' or 'ymd'. (Default is 'ymd' for ISO 8601 compability) * @return array [ year, month, day ] * @since 1.0 */ function datecheck($date, $yearepsilon=5000, $format='ymd') { $format = strtolower($format); if (count($datebits=explode('-',$date))!=3) return false; $year = intval($datebits[strpos($format, 'y')]); $month = intval($datebits[strpos($format, 'm')]); $day = intval($datebits[strpos($format, '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)))) return false; // date out of range return array( 'year' => $year, 'month' => $month, 'day' => $day ); } ?>

« previous php.notes (#115212) next »