Possible Duplicate:
How to check if a string is a legal “dd/mm/yyyy” date?
I have a a regular text field that I want to validate that im using for date of birth.
it's like dd/mm/yyyy. Im wondering how i can check that the user entered the date in the correct format with the slashes. I have a php script to calculate the age from the entered data and strips the slashes in order to do so. I figure i'd run with that, but just in case a user e开发者_如何学Cnters it wrong, well lol, you know how that is...
Any ideas?
if (!preg_match('#^(\d{2})/(\d{2})/(\d{4})$#', $date, $matches)) {
die('Invalid format');
}
$dob = mktime(0, 0, 0, $matches[2], $matches[1], $matches[3]);
if (date('d/m/Y', $dob) != $date) {
die('Invalid date');
}
echo 'User was born on ' . date('l, F jS, Y', $dob);
精彩评论