tags:

views:

49

answers:

1

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.

+1  A: 

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.
{
    ....
}
Christopher Gutteridge
And your <label> doesn't match your <input> and in the <input> it's bad practice to have a differing id and name attribute.
Christopher Gutteridge
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?
Newb
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: http://blogs.ecs.soton.ac.uk/webteam/2009/11/07/date-formats/
Christopher Gutteridge