Hi,
I am puzzling on how to do this:
Suppose I was born November 22nd, 1981, and I want to be able to see how many years, months and days I have lived. I produced this in a quickie:
$bYear = 1981;
$bMonth = 11;
$bDay = 22;
// Year calculation
$dYear = date("Y") - $bYear; // 2001 - 1981 = 20
if(date("n") < $bMonth) { // if current month is smaller than month of bday
$dYear = $dYear - 1; // substract one year
} elseif($cMonth = $bMonth) { // if current month is equal to month of bday
if(date("j") < $bDay) { // and if current day is equal to birthday
$dYear = $dYear - 1; // substract one year (birthday event 🙂)
} // end if
} // end if
// Month calculation
$dMonth = date("n") - $bMonth; // 1 - 11 = -10
if($dMonth < 0) { // if negative (this makes sense, think about it)
$dMonth = $dMonth + 12; // add 12 (so $dMonth is now two)
} // end if
// Day calculation
$dDay = date("j") - $bDay; // 17 - 22 = -5
if($dDay < 0) { // if negative (...)
$dDay = $dDay / -1; // divide by -1 to get positive.
// This however is flawed.
} // end if
echo "$dYear years";
if($dMonth <> 0) { // aka if current month is not bday month
echo ", $dMonth months ";
}
if($dDay <> 0) { // aka if current day is not bday day
echo "and $dDay days";
}
echo " old."
Now obviously the dDay mechanism is flawed, but I see no way of fixing it. I've probably been staring at it for too long 🙂
Clues, anyone?
Thanks!
Regards,
infernix