get age by birthdate
- July 23rd, 2007
- php, Snippet-library
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>
















One comment to “get age by birthdate”
- July 23rd, 2007
- Joris
July 23rd, 2007 at 11:00 pmI was searching for this source code.
Thank you ;)
Submit your comment