I am trying to validate a date in PHP but I am getting an “Invalid date format” error even though I supplied the correct date format. My PHP version is 5.3.8.
Here is my code, please let me know what you think:
try
{
$validDate = new DateTime("2/2/2011");
echo "valid format";
}
catch(Exception $e)
{
echo "Date Incident Occurred: Invalid Date format!";
}
Have you tried inspecting the exception? It’s trying very hard to tell you what happened. That’s its entire reason for existence. If you really are supplying a valid date, I will bet the exception is telling you something like this: