I need to get the age of a person. In case of an infant <= 7 days the output should be done in days. If the person is > 2 months and < 1 year the output should be months.
Here I got the problem, that some months are 31 other 30 or 28 days, so my solution isn't exact. Same problem for the else-case: Years with 366 days are ignored by my attempt, so the age isn't calculated correctly.
$timestamp = time();
$birthday_timestamp = mktime(0, 0, 0, $month, $day, $year);
$difference = $timestamp - $birthday_timestamp;
if ($difference < 1209600) return $output = ($difference / 86400)." days";
elseif ($difference < 5184000) return $output = ($difference / 86400 * 7). " weeks";
elseif ($difference < 31536000) return $output = ($difference / 86400 * 30). " months";
else return $output = ($difference / 86400 * 365). " years";