note 102539 added to function.checkdate

From: Date: Sun, 20 Feb 2011 12:30:48 +0000
Subject: note 102539 added to function.checkdate
Groups: php.notes 
Request: Send a blank email to php-notes+get-177153@lists.php.net to get a copy of this message
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; } ?> ---- Server IP: 69.147.83.197 Probable Submitter: 85.229.96.10 ---- Manual Page -- http://www.php.net/manual/en/function.checkdate.php Edit -- https://master.php.net/note/edit/102539 Del: integrated -- https://master.php.net/note/delete/102539/integrated Del: useless -- https://master.php.net/note/delete/102539/useless Del: bad code -- https://master.php.net/note/delete/102539/bad+code Del: spam -- https://master.php.net/note/delete/102539/spam Del: non-english -- https://master.php.net/note/delete/102539/non-english Del: in docs -- https://master.php.net/note/delete/102539/in+docs Del: other reasons-- https://master.php.net/note/delete/102539 Reject -- https://master.php.net/note/reject/102539 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#177153) next »