Validate your date
if (DateTime::createFromFormat('d/m/y', $date)) {
echo "Date is valid";
} else {
echo "Date is not valid";
}
More generally, if you want to validate "stuff"
Use PHP "embedded filters" with filter_var()
. Link. You can use it for a lot of "standard" filters or with your regex. In the case of dd/mm/yy
dates you can use a custom regex:
$regex = '~^\d{2}/\d{2}/\d{2}$~';
print filter_var("01/01/2017",FILTER_VALIDATE_REGEXP,["options" => ["regexp"=> $regex]]);
Beware, this will validate also 22/22/22
which is not correct, so use the first proposed date validation, I used it only as example ;)