note 102539 deleted from function.checkdate by danbrown

From: Date: Sun, 20 Feb 2011 15:44:00 +0000
Subject: note 102539 deleted from function.checkdate by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-177159@lists.php.net to get a copy of this message
Note Submitter: waldemar dot axdorph at gmail dot com ---- amcanadian1973's script will not handle dates like 2011-02-30 i.e. false dates. This is because strtotime will convert 2011-02-30 to 2011-03-02. The script then handles the date 2011-03-02 and not the given date (2011-02-30). This is my take on it. Please observe that this script only handles the YYYY-MM-DD format. <?php /* var_dump( is_date('2011-02-20') ); // today => bool(true) var_dump( is_date('2011-02-30') ); // non-existent => bool(false) var_dump( is_date('2012-02-29') ); // leap day => bool(true) var_dump( is_date('2011-2-22') ); // malformed => bool(false) var_dump( is_date('YYYY-MM-DD') ); // characters => bool(false) */ function is_date( $str, $delimiter = '-' ) { $str = trim($str); if(strlen($str) != 10) // strlen('YYYY-MM-DD') == 10 { return false; } $stamp = strtotime( $str ); //ok for first defense if ( !is_numeric( $stamp ) ) { return false; } $dateParts = explode( $delimiter, $str );//explode the date by its delimiter $year = $dateParts[0]; $month = $dateParts[1]; $day = $dateParts[2]; if (checkdate( $month, $day, $year )) { return true; } return false; } ?>

« previous php.notes (#177159) next »