Jump to content

[SOLVED] Work out age from date of birth.


DeanWhitehouse

Recommended Posts

@rajivgonsalves

I used your code

$day = trim(htmlentities($_POST['age']));
$month =trim(htmlentities($_POST['age1']));
$year =trim(htmlentities($_POST['age2']));
$dob = $day.$month.$year;

//Work out birthday
$strDate = $dob;
echo floor((time()-strtotime(substr($strDate,4,4)."-".substr($strDate,2,2)."-".substr($strDate,0,2)))/(86400*365));
//end bday

 

but this works for my birthday and probably more, but when i submitted a form with the birthday being

01011920

it got the age 38.

 

any ideas?

You just need years, right?

 

Different solution of the one earlier presented.  In my opinion, this one is cleaner.  Not sure if it's more efficient.

 

<?php

$month = 2;
$day = 18;
$year = 1992;

$tyear = (int) date('Y');
$tmonth = (int) date('n');
$tday = (int) date('j');
$years = $tyear - $year - 1;
if($tmonth > $month || ($tmonth == $month && $tday >= $day)) {
//oddly written, but i think that's the most effecient way to do it.
++$years;
}
//if the current date is past (or equal to) the birthday, add a year

echo $years;

?>

 

 

Edit:  Bleh, just realized the "if($month <= date('n') && $day <= date('j')) ++$years;" part is wrong.  Since you already have a solution, I'm not gonna bother fixing it, unless you're interested in this.

 

 

Edit:  Fixed it, because it bothered me not having it correct. lol

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.