开发者

php issues with date validation

开发者 https://www.devze.com 2022-12-13 02:52 出处:网络
开发者_JAVA百科Never mind I did not try hard enough, here is what I was looking for: $dob = \'1980-09-04\';
开发者_JAVA百科

Never mind I did not try hard enough, here is what I was looking for:

$dob = '1980-09-04';
echo $dob;
$age = date('Y') - date('Y', strtotime($dob));
echo $age;
if (date('md') < date('md', strtotime($dob))) {
    $age--;
    echo $age;
}

Sorry for the unnecessary question.


Checking 21 yrs old.... is this for pr0n?

First of all, MM/DD/YYYY is harmful if you are writing something for use internationally! Better to write

<select name='month'><option value='01'>Jan</option>....
<input name='day' maxlength='2' size='2' />
<input name='year' maxlength='4' size='4' />

As that won't confuse us Europeans.

Secondly, you're asking for their DOB and storing it an a parameter & var named "age" which is going to bite you on the arse if you every try to maintain the code later.

Thirdly, your example data does not match the required input. You tell them an example with separators and then actually process 8 digits with no spaces.

Actual answer: However, if you want to validate the above icky MMDDYYYY 8 digit string try:

if( !preg_match( "/^\d{8}$/", $dob ) ) # fail if is not a string of 8 numeric digits.
{
    ....
}
0

精彩评论

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