Age and DOB with Registration Form
Moderator: General Moderators
Age and DOB with Registration Form
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
------------------------------------
Web Developer | Software Developer
https://kalebklein.com
PGP Key: https://keybase.io/pazuzu156
Re: Age and DOB with Registration Form
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)
Assuming you are using a mySQL date format (YYYY-MM-DD) you can use:
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 );
}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
Thanks alot. 
- Kaleb Klein
------------------------------------
Web Developer | Software Developer
https://kalebklein.com
PGP Key: https://keybase.io/pazuzu156
------------------------------------
Web Developer | Software Developer
https://kalebklein.com
PGP Key: https://keybase.io/pazuzu156