Calculating the age of a person.

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
waskelton4
Forum Contributor
Posts: 132
Joined: Mon Sep 09, 2002 6:42 pm

Calculating the age of a person.

Post 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";
  
 }
User avatar
Heavy
Forum Contributor
Posts: 478
Joined: Sun Sep 22, 2002 7:36 am
Location: Viksjöfors, Hälsingland, Sweden
Contact:

Post 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.
waskelton4
Forum Contributor
Posts: 132
Joined: Mon Sep 09, 2002 6:42 pm

Post 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
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

Code: Select all

$yearsSec = ($byear - 1900)* (365.26*24*60*60);
i thought a year has 365.25 days:/
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post 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.
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post 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
Post Reply