Ec (2); & nbsp; & lt ;? Php & nbsp; ** & nbsp; * Validateadate & nbsp; & nbsp; * @ param & nbsp; string & nbsp; $ script ec (2); script
/**
*
* Validate a date
*
* @ Param string $ date
* @ Param string format
* @ Return bool
*
*/
Function validateDate ($ date, $ format = 'yyyy-MM-DD ')
{
Switch ($ format)
{
Case 'yyyy/MM/dd ':
Case 'yyyy-MM-DD ':
List ($ y, $ m, $ d) = preg_split ('/[-./]/', $ date );
Break;
Case 'yyyy/DD/mm ':
Case 'yyyy-DD-MM ':
List ($ y, $ d, $ m) = preg_split ('/[-./]/', $ date );
Break;
Case 'dd-MM-YYYY ':
Case 'dd/MM/yyyy ':
List ($ d, $ m, $ y) = preg_split ('/[-./]/', $ date );
Break;
Case 'Mm-DD-YYYY ':
Case 'Mm/DD/yyyy ':
List ($ m, $ d, $ y) = preg_split ('/[-./]/', $ date );
Break;
Case 'yyyymmdd ':
$ Y = substr ($ date, 0, 4 );
$ M = substr ($ date, 4, 2 );
$ D = substr ($ date, 6, 2 );
Break;
Case 'yyyyddmm ':
$ Y = substr ($ date, 0, 4 );
$ D = substr ($ date, 4, 2 );
$ M = substr ($ date, 6, 2 );
Break;
Default:
Throw new Exception ("Invalid Date Format ");
}
Return checkdate ($ m, $ d, $ y );
}
?>
Example Usage
Echo validateDate ('2017-04-21 ')? 'Good'. "": 'bad '."";
Echo validateDate ('2017-21-04 ', 'yyyy-DD-MM ')? 'Good'. "": 'bad '."";
Echo validateDate ('2017-21-04 ', 'yyyy/DD/mm ')? 'Good'. "": 'bad '."";
Echo validateDate ('2014/1/123', 'dd/MM/yyyy ')? 'Good'. "": 'bad '."";
Echo validateDate ('2014/1/123', 'Mm/DD/yyyy ')? 'Good'. "": 'bad '."";
Echo validateDate ('20140901', 'yyyymmdd ')? 'Good'. "": 'bad '."";
Echo validateDate ('20140901', 'yyyyddmm ')? 'Good'. "": 'bad '."";
?>