note 69693 added to function.checkdate

From: Date: Fri, 15 Sep 2006 17:45:27 +0000
Subject: note 69693 added to function.checkdate
Groups: php.notes 
Request: Send a blank email to php-notes+get-117229@lists.php.net to get a copy of this message
I wrote this function based on the ones showed above but adding extra flexibility so you can use YY-MM-DD. DD-MM-YY, YYYY-MM-DD, YYYY-DD-MM and all other combinations and also dots, slashes and commas as separators. function datetime_check_date_string($date_string) { $result = false; /* Checking for a match in the form MM-DD-YY(YY) or DD-MM-YY(YY) with alternative separators */ if (ereg('^([0-9]{1,2})[-,/.]([0-9]{1,2})[-,/.](([0-9]{2})|([0-9]{4}))$', $date_string)) { $date_array = split('[-,/.]', $date_string); /* Let's try MM-DD */ $m=$date_array[0]; $d=$date_array[1]; $y=$date_array[2]; $result = checkdate($m, $d, $y); /* MM-DD failed, let's try DD-MM */ if ($result===false) { $m=$date_array[1]; $d=$date_array[0]; $y=$date_array[2]; $result = checkdate($m, $d, $y); } /* Checking for a match in the form YY(YY)-MM-DD or YY(YY)-DD-MM with alternative separators */ } elseif (ereg('^(([0-9]{2})|([0-9]{4}))[-,/.]([0-9]{1,2})[-,/.]([0-9]{1,2})$', $date_string)) { $date_array = split('[-,/.]', $date_string); /* Let's try MM-DD */ $m=$date_array[1]; $d=$date_array[2]; $y=$date_array[0]; $result = checkdate($m, $d, $y); /* MM-DD failed, let's try DD-MM */ if ($result===false) { $m=$date_array[2]; $d=$date_array[1]; $y=$date_array[0]; $result = checkdate($m, $d, $y); } } return $result; } ---- Server IP: 66.163.161.117 Probable Submitter: 201.230.224.223 ---- Manual Page -- http://www.php.net/manual/en/function.checkdate.php Edit -- https://master.php.net/note/edit/69693 Del: integrated -- https://master.php.net/note/delete/69693/integrated Del: useless -- https://master.php.net/note/delete/69693/useless Del: bad code -- https://master.php.net/note/delete/69693/bad+code Del: spam -- https://master.php.net/note/delete/69693/spam Del: non-english -- https://master.php.net/note/delete/69693/non-english Del: in docs -- https://master.php.net/note/delete/69693/in+docs Del: other reasons-- https://master.php.net/note/delete/69693 Reject -- https://master.php.net/note/reject/69693 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#117229) next »