0

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.

6
  • 1
    Hint: You should post what exactly you want to achieve and what exactly is going wrong... Commented Dec 1, 2009 at 12:49
  • 1
    and the what does not do what you expect it to do? Commented Dec 1, 2009 at 12:50
  • 1
    Btw: If you require the date to be in the format mm/dd/yyyy, then it is kind of wrong to set the maxlength of the input field to 8. Commented Dec 1, 2009 at 12:50
  • 1
    and checking 'numeric' on an input with slashes will not work either Commented Dec 1, 2009 at 12:52
  • 1
    Would it not be better to capture the date in separate fields? Commented Dec 1, 2009 at 12:54

1 Answer 1

1

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.
{
    ....
}
Sign up to request clarification or add additional context in comments.

3 Comments

And your <label> doesn't match your <input> and in the <input> it's bad practice to have a differing id and name attribute.
I just changed the code above, thanks for the idea of separating the month, day, year, but I wanted just one variable to call from the php script, $dob?
Just bite the bullet and use 3 variables! You're right in using "YYYY-MM-DD" as the internal format. See my rant on the subject: blogs.ecs.soton.ac.uk/webteam/2009/11/07/date-formats

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.