/ Published in: PHP
Calculate age in years based on a timestamp and a reference timestamp (which defaults to the current time when left empty)
Expand |
Embed | Plain Text
/** * Calculate age in years based on timestamp and reference timestamp * If the reference $now is set to 0, then current time is used * * @param int $timestamp * @param int $now * @return int */ function calculateAge($timestamp = 0, $now = 0) { # default to current time when $now not given if ($now == 0) # calculate differences between timestamp and current Y/m/d # check if we already had our birthday if ($monthDiff < 0) $yearDiff--; elseif (($monthDiff == 0) && ($dayDiff < 0)) $yearDiff--; # set the result: age in years # deliver the result return $result; }
You need to login to post a comment.
