This php function calculates the age in years for a given birthdate in the format YYYY-MM-DD.

This php function calculates the age in years for a given birthdate in the format YYYY-MM-DD.
 
<code class="prettyprint">
function getAge($birthdate)
{
    // get month, day, year
    $tmp = split('-', $birthdate);
    $month = (int)$tmp[1]; $day = (int) $tmp[2]; $year = (int) $tmp[0];
 
    $current_day_of_year = 1 + date('z');
    $user_day_of_year = 1 + date('z', mktime(1,1,1,$month, $day, $year));
    $years = date('Y') - $year;
 
    // we calculate based on birth day, if we haven't reached user's birthday yet,
    // substract one year
    if ($current_day_of_year < $user_day_of_year) {
        $years--;
    }
 
    return $years;
}
</code></code>

Bookmark this!

These icons link to social bookmarking sites where readers can share and discover new web pages.
  • blinkbits
  • BlinkList
  • blogmarks
  • co.mments
  • del.icio.us
  • Digg
  • Fark
  • Furl
  • MisterWong
  • NewsVine
  • Reddit
  • Spurl
  • StumbleUpon
  • TailRank
  • Technorati

One comment to “get age by birthdate”

I was searching for this source code.
Thank you ;)

Submit your comment

vision22.net

Copyright © 2007