Age and DOB with Registration Form

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Pazuzu156
Forum Contributor
Posts: 241
Joined: Sat Nov 20, 2010 9:00 pm
Location: GA, USA
Contact:

Age and DOB with Registration Form

Post 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?
- Kaleb Klein
------------------------------------
Web Developer | Software Developer
https://kalebklein.com
PGP Key: https://keybase.io/pazuzu156
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: Age and DOB with Registration Form

Post 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 );
}
User avatar
Pazuzu156
Forum Contributor
Posts: 241
Joined: Sat Nov 20, 2010 9:00 pm
Location: GA, USA
Contact:

Re: Age and DOB with Registration Form

Post by Pazuzu156 »

Thanks alot. :)
- Kaleb Klein
------------------------------------
Web Developer | Software Developer
https://kalebklein.com
PGP Key: https://keybase.io/pazuzu156
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Age and DOB with Registration Form

Post by Celauran »

See also str_pad()
Post Reply