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..
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";
}