Monday, 2 February 2015

Calculate How Many Years Old

Calculate How Many Years Old

This simple function calulates the age, in years, of a date supplied in any format supported by the strtotime() funcition, which means, basically, any date you can sanely imagine.

<?php
/**
 *
 * @Get how many years old
 *
 * @param string
 *
 * @return int
 *
 */
function yearsOld($birthday)
{
    if ((
$birthday strtotime($birthday)) === false)
    {
        return 
false;
    }
    for (
$i 0strtotime("-$i year") > $birthday; ++$i);
    return 
$i 1;
}  



 
/*** example usage ***/
 
$birthday 'april 20 1961';
 
 echo 
yearsOld($birthday).' years old';
 
?>

0 comments:

Post a Comment