Page 1 of 1

Age and DOB with Registration Form

Posted: Wed Jun 15, 2011 4:44 pm
by Pazuzu156
I have a registration form, and instead of manually inputting age, I want them to just use the DOB, and it auto calculate their ages. And update on their birthday, removing the hassle of having to update their age themselves. How would I do this in PHP?

Re: Age and DOB with Registration Form

Posted: Wed Jun 15, 2011 11:43 pm
by twinedev
Here is a bit of code I had found and modified to allow you to just pass direct month/day values with out requiring they be 2 digits for under 10 (ie, use 8 instead of 08)

Code: Select all

function currentAge($m,$d,$y) {
	$md = (($m<10)?'0'.$m:$m) . (($d<10)?'0'.$d:$d);
	return (date("md") < $md ? date("Y")-$y-1 : date("Y")-$y );
}
Assuming you are using a mySQL date format (YYYY-MM-DD) you can use:

Code: Select all

function currentAge($sqlDate) {
	list($y,$m,$d) = explode('-',$sqlDate);
	$md = $m.$d; // Will already have leading 0's from above
	return (date("md") < $md ? date("Y")-$y-1 : date("Y")-$y );
}

Re: Age and DOB with Registration Form

Posted: Thu Jun 16, 2011 12:19 am
by Pazuzu156
Thanks alot. :)

Re: Age and DOB with Registration Form

Posted: Thu Jun 16, 2011 9:16 am
by Celauran
See also str_pad()