Page 1 of 1

Calculating the age of a person.

Posted: Tue Oct 22, 2002 2:12 pm
by waskelton4
Hey folks..

I'm working on trying to get my app to pull a Date-Of-Birth from my mySQL table and from that caluculate the age of the person it's associated with. It wouldn't be a big problem for me except for one thing..

If the age of the person is less that two years, the age needs to be displayed in months, and if it's less that 2 months, needs to be displayed in weeks.. I started out with my calculations and then began to realize that i was headed in the WRONG direction..

I am storing my date in a yyyy-mm-dd format.

will i need to convert the difference into days and then do my calculations, or can it be done comparing two dates in the format above??

Any help is greatly appreciated..

Thanks
Will

Here's what i've started with... but it's probalby not even worth reading.. :roll:

Code: Select all

function getAge ($date) {
    
$chunks=explode("-","$date"); 
$yearThen="$chunksї0]"; 
$monthThen="$chunksї1]"; 
$dayThen="$chunksї2]"; 

$yearNow = date("Y");
$monthNow = date("m");
$dayNow = date("d");

$yearDiff = $yearNow - $yearThen;
$monthDiff = $monthNow - $monthThen;
$dayDiff = $dayNow - $dayThen

if ($yearDiff >= 2)
{ 
$age = yearDiff." yrs";
return $age;
}
elseif ($yearDiff == 1)
{ 
$age = $monthDiff + 12;
$age = $age." Mo";
return $age;
}
elseif ($yearDiff == 0 && $monthDiff > 2)
{
$age = $monthDiff." Mos";
  
 }

Posted: Tue Oct 22, 2002 4:39 pm
by Heavy
Why not use seconds?
The following comes from the manual:
int strtotime ( string time [, int now])

The function expects to be given a string containing an English date format and will try to parse that format into a UNIX timestamp relative to the timestamp given in now, or the current time if none is supplied. Upon failure, -1 is returned.

Note: The valid range of a timestamp is typically from Fri, 13 Dec 1901 20:45:54 GMT to Tue, 19 Jan 2038 03:14:07 GMT. (These are the dates that correspond to the minimum and maximum values for a 32-bit signed integer.)

**********************
Feed time with correctly formatted data and have a unix timestamp returned.

Then you can substract date of birth from now and have a result in seconds, which can be converted into something readable with string strftime ( string format [, int timestamp]).

Do some range checking to decide how the output should be done.

NOTE: I haven't tried it. Hopefully, it works.

Posted: Fri Oct 25, 2002 4:18 pm
by waskelton4
Well...

I got a huge headache but i think i figured it out.. my test data seems to work... maybe some of the real programmers out there can take a look at it and tell me if it works...

here's what i finished with..

Code: Select all

function getAge($dob)
{
$chunks=explode("-","$dob"); 
$byear="$chunksї0]"; 
$bmonth="$chunksї1]"; 
$bday="$chunksї2]"; 

$yearsSec = ($byear - 1900)* (365.26*24*60*60);
$monthsSec = ($bmonth)* (60*60*24*30);
$daysSec = ($bday)*(60*60*24);

$nowSecs = ((date('Y')-1900)*(365.26*24*60*60))  +  ((date('m'))* (60*60*24*30))  +  ((date('d'))*(60*60*24));
$dobSecs = $yearsSec + $monthsSec + $daysSec;
$ageSecs = $nowSecs - $dobSecs;

$dobSecs = '';

if($ageSecs >= (365.26*24*60*60*2))
   {$age = intval($ageSecs/(365.26*24*60*60))."yr";}
elseif($ageSecs > (60*60*24*2*30) && $ageSecs < (365.26*24*60*60*2))
	&#123;$age = intval($ageSecs/(60*60*24*30))."mo";&#125;
else
	&#123;$age = intval($ageSecs/(60*60*24*7))."wk";&#125;
echo $age;
&#125;
thanks
will

Posted: Fri Oct 25, 2002 4:28 pm
by qads

Code: Select all

$yearsSec = ($byear - 1900)* (365.26*24*60*60);
i thought a year has 365.25 days:/

Posted: Fri Oct 25, 2002 4:33 pm
by hob_goblin

Code: Select all

<?


$str = "2000-10-25";

function getage($date)&#123;
$olddate = strtotime($date);
$today = time();
$dif = $today - $olddate;
$dif = $dif / 365.25;
$dif = ((($dif / 24) / 60) / 60);
$dif = round($dif);
return $dif;&#125;

echo "$str was ".getage($str)." years ago.";


?>
check it out.

Posted: Fri Oct 25, 2002 4:37 pm
by jason
If you wanted to get your age in SQL (which is what I like doing):

http://www.mysql.com/doc/en/Date_calculations.html