age/age.functions.php
<?php
/**
* Calculates age by using... math.
*
* @author Torleif Berger
* @link http://www.geekality.net/?p=1397
* @link http://00f.net/2007/06/04/an-age-is-not-a-duration/
* @license http://creativecommons.org/licenses/by/3.0/
*
* @param timestamp date of birth, 'yyyy-mm-dd'
* @param timestamp now, 'yyyy-mm-dd'. defaults to `time()`
* @return number the age
*/
function getAge1($birth, $now = NULL)
{
$now = getdate($now === NULL ? time() : $now);
$birth = getdate($birth);
$age = $now['year'] - $birth['year'];
if($now['mon'] < $birth['mon']
|| ($now['mon'] == $birth['mon'] && $birth['mday'] > $now['mday']))
{
$age -= 1;
}
return $age;
}
/**
* Calculates age by using diff function.
*
* @author Torleif Berger
* @link http://www.geekality.net/?p=1397
* @link http://www.php.net/manual/en/datetime.diff.php
* @license http://creativecommons.org/licenses/by/3.0/
*
* @param string date of birth, 'yyyy-mm-dd'
* @param string now, 'yyyy-mm-dd'. defaults to `time()`
* @return number the age
*/
function getAge2($birth, $now = NULL)
{
$now = new DateTime($now);
$birth = new DateTime($birth);
return $birth->diff($now)->format('%r%y');
}