开发者

how to validate a field for DOB using forward slashes dd/mm/yyyy [duplicate]

开发者 https://www.devze.com 2023-04-11 02:58 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: How to check if a string is a legal “dd/mm/yyyy” date?
This question already has answers here: Closed 11 years ago.

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);
0

精彩评论

暂无评论...
验证码 换一张
取 消