Function to validate date format in PHP


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. function checkDateFormat($date)
  2. {
  3. //match the format of the date
  4. if (preg_match ("/^([0-9]{4})-([0-9]{2})-([0-9]{2})$/", $date, $parts))
  5. {
  6. //check weather the date is valid of not
  7. if(checkdate($parts[2],$parts[3],$parts[1]))
  8. return true;
  9. else
  10. return false;
  11. }
  12. else
  13. return false;
  14. }
  15.  
  16. echo checkDateFormat("2008-02-29"); //return true
  17. echo checkDateFormat("2007-02-29"); //return false

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.