Tagged: check

Date format validation in PHP

- by admin

In case of date format checking in PHP just use this function:
function checkDateFormat($date)
{
  //match the format of the date
  if (preg_match ("/^([0-9]{4})-([0-9]{2})-([0-9]{2})$/", $date, $parts))
  {
    //check weather the date is valid of not
        if(checkdate($parts[2],$parts[3],$parts[1]))
          return true;
        else
         return false;
  }
  else
    return false;
}

Example:
echo checkDateFormat("2008-02-29"); //return true
echo checkDateFormat("2007-02-29"); //return false

« All tags