I am trying to check a date format to see if I can check the data variable has certain format like MM-DD-YYYY. if not, then exit(). I am not sure how to check the format and would appreciate if any one can help me about it. Thanks…
$date=05/25/2010;
if(XXXXX){
// do something....
}
Use a regular expression, as others have suggested. The ones posted before will accept invalid dates such as 99/99/9999 however. Here’s an improved regex (lifed from here)
It will only take valid dates and it will also accept different separators (such as 05.20.2002 and 05-02-2002).
If you ask me it’s bad user experience to force them to enter a particular format. YOU can handle different formats using strtotime().