DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Michael has posted 27 posts at DZone. View Full User Profile

Calculate Age

01.28.2006
| 116415 views |
  • submit to reddit
        With this function you can calculate the age of a person

Example:
echo "Age is: " . birthday ("1984-07-05");

Result will be (23 Feb 2005) = "Age is: 20"

<?php

  //calculate years of age (input string: YYYY-MM-DD)
  function birthday ($birthday){
    list($year,$month,$day) = explode("-",$birthday);
    $year_diff  = date("Y") - $year;
    $month_diff = date("m") - $month;
    $day_diff   = date("d") - $day;
    if ($day_diff < 0 || $month_diff < 0)
      $year_diff--;
    return $year_diff;
  }

?>
    

Comments

Snippets Manager replied on Wed, 2011/06/08 - 1:09am

This could be wrong (I haven't put it through it's proper paces yet) but for those that want something simple like the first post that actually calculates properly you should try this (I have very little experience programming so it is very fundamental): function get_age($Birthdate){ if (empty($Birthdate)){ return false; } list($BirthYear,$BirthMonth,$BirthDay) = explode("-", $Birthdate); $YearDiff = date("Y") - $BirthYear; $MonthDiff = date("m") - $BirthMonth; $DayDiff = date("d") - $BirthDay; // If the birthday has not occured this year if ($MonthDiff == 0){ if($DayDiff < 0) $YearDiff--; } if ($MonthDiff < 0){ $YearDiff--; } return $YearDiff; }

Snippets Manager replied on Fri, 2011/01/14 - 10:41am

ups... last post was not formatted. public static function calculateFriendlyAge($unformattedDate) { // array that hold the year, month and days text $friendlyAgeText = array(); // internal class for get the date $oMD = new MagicDate($unformattedDate); $yearDiff = date("Y") - $oMD->getYear(); $monthDiff = date("m") - $oMD->getMonth(); $dayDiff = date("d") - $oMD->getDay(); if ($monthDiff < 0) { // trick: now is January, and the birthdate on December $yearDiff--; $monthDiff += 12; } if ($dayDiff < 0) { // trick: now is 20, and the birthdate is past Month, on 25. $monthDiff--; $dayDiff += 30; // dirty: no support for 28-29 days on february, 31 days on some months } // the age is already numeric, now convert to friendly text switch ($yearDiff): case 0: break; case 1: $friendlyAgeText[] = "1 year"; break; default: $friendlyAgeText[] = $yearDiff . " years"; break; endswitch; if ($yearDiff <= 4) { // support "with months" for child born 4 years ago switch ($monthDiff): case 0: break; case 1: $friendlyAgeText[] = "1 month"; break; default: $friendlyAgeText[] = $monthDiff . " months"; break; endswitch; if ($yearDiff == 0 && $monthDiff <= 3 && $dayDiff > 0) { // support "with days" for baby born 3 months ago $friendlyAgeText[] = $dayDiff . " days"; } } return join(" and ", $friendlyAgeText); }

Snippets Manager replied on Fri, 2011/01/14 - 10:41am

/* convert age in friendly text. Support "and days" for babies born 3 months ago. And, support for "and x months" for children born 4 years ago. eg: - 3 days - 1 month and 15 days - 6 months - 1 year and 4 months - 5 years */ public static function calculateFriendlyAge($unformattedDate) { // array that hold the year, month and days text $friendlyAgeText = array(); // internal class for get the date $oMD = new MagicDate($unformattedDate); $yearDiff = date("Y") - $oMD->getYear(); $monthDiff = date("m") - $oMD->getMonth(); $dayDiff = date("d") - $oMD->getDay(); if ($monthDiff < 0) { // trick: now is January, and the birthdate on December $yearDiff--; $monthDiff += 12; } if ($dayDiff < 0) { // trick: now is 20, and the birthdate is past Month, on 25. $monthDiff--; $dayDiff += 30; // dirty: no support for 28-29 days on february, 31 days on some months } // the age is already numeric, now convert to friendly text switch ($yearDiff): case 0: break; case 1: $friendlyAgeText[] = "1 year"; break; default: $friendlyAgeText[] = $yearDiff . " years"; break; endswitch; if ($yearDiff <= 4) { // support "with months" for child born 4 years ago switch ($monthDiff): case 0: break; case 1: $friendlyAgeText[] = "1 month"; break; default: $friendlyAgeText[] = $monthDiff . " months"; break; endswitch; if ($yearDiff == 0 && $monthDiff <= 3 && $dayDiff > 0) { // support "with days" for baby born 3 months ago $friendlyAgeText[] = $dayDiff . " days"; } } return join(" and ", $friendlyAgeText); }

Snippets Manager replied on Thu, 2012/02/16 - 4:59pm

$birth = strtotime($data['year'].'-'.$data['month'].'-'.$data['day']); $age_in_seconds = time() - $birth; $age = floor($age_in_seconds / 31556926); // 31 556 926 seconds in year

Snippets Manager replied on Wed, 2009/09/23 - 6:32am

calamus77's version with tight ternary operators FTW!

Snippets Manager replied on Fri, 2008/12/26 - 10:12pm

Of course, the version above only lasted until the New Year. This new version now copes with determining age in months across the Dec-Jan divide. It also arbitrarily tightens up the accuracy 'within' a month. function GetAge($DOB, $DOD) { // Get current date $CD = date("Y-n-d"); list($cY,$cm,$cd) = explode("-",$CD); // Get date of birth list($bY,$bm,$bd) = explode("-",$DOB); // is there a date of death? if ($DOD!="" && $DOD != "0000-00-00") { // Animal is dead list($dY,$dm,$dd) = explode("-",$DOD); if ($bY == $dY) { $months = $dm - $bm; if ($months == 0 || $months > 1) { return "$months months"; } else return "$months month"; } else $years = ( $dm.$dd < $bm.$bd ? $dY-$bY-1 : $dY-$bY ); if ($years == 0 || $years > 1) { return "$years years"; } else { return "$years year"; } } else { // Animal is alive if ($bY != "" && $bY != "0000") { if ($bY == $cY) { // Birth year is current year $months = $cm - $bm; if ($months == 0 || $months > 1) { return "$months months"; } else return "$months month"; } else if ($cY - $bY == 1 && $cm - $bm < 12) { // Born within 12 months, either side of 01 Jan //Determine days and therefore proportion of month if ($cd - $bd > 0) { $xm = 0; } else { $xm = 1; } $months = 12 - $bm + $cm - $xm; if ($months == 0 || $months > 1) { return "$months months"; } else { return "$months month"; } } // Animal older than 12 months, return in years $years = (date("md") < $bm.$bd ? date("Y")-$bY-1 : date("Y")-$bY ); if ($years == 0 || $years > 1) { return "$years years"; } else { return "$years year"; } } else return "No Date of Birth!"; } }

Snippets Manager replied on Fri, 2008/12/26 - 10:12pm

I have modified this code for my cattle stud database. It covers the 'infants' and gives the age at death of those that have died. function GetAge($DOB, $DOD) { list($bY,$bm,$bd) = explode("-",$DOB); if ($DOD!="" && $DOD != "0000-00-00") { // Animal is dead list($dY,$dm,$dd) = explode("-",$DOD); if ($bY == $dY) { $months = $dm - $bm; if ($months == 0 || $months > 1) { return "$months months"; } else return "$months month"; } else return( $dm.$dd < $bm.$bd ? $dY-$bY-1 : $dY-$bY ); } else // Animal is alive if ($bY != "" && $bY != "0000") { if ($bY == date("Y")) { $months = date('m') - $bm; if ($months == 0 || $months > 1) { return "$months months"; } else return "$months month"; } else return( date("md") < $bm.$bd ? date("Y")-$bY-1 : date("Y")-$bY ); } else return "No Date of Birth!"; }

Snippets Manager replied on Sun, 2008/08/17 - 10:03pm

There was a bug in my code that didn't anticipate infants. Here's the corrected version: function getAge( $p_strDate, $e_strDate ) { list($Y,$m,$d) = explode("-",$p_strDate); if ($Y != "" && $Y != "0000") { if ($Y == date("Y")) { $months = date('m') - $m; if ($months == 0 || $months > 1) return "$months months old"; else return "$months month old"; } else { if ($e_strDate!="" && $e_strDate != "0000-00-00") { list($eY,$em,$ed) = explode("-",$e_strDate); return( $em.$ed < $m.$d ? $eY-$Y-1 : $eY-$Y ); } else return( date("md") < $m.$d ? date("Y")-$Y-1 : date("Y")-$Y ); } } else return "???"; }

Snippets Manager replied on Sun, 2008/08/17 - 10:03pm

I've upgraded your getAge function to include a death date and account for a null date. function getAge( $p_strDate, $e_strDate ) { list($Y,$m,$d) = explode("-",$p_strDate); if ($Y != date("Y") && $Y != "" && $Y != "0000") { if ($e_strDate!="" && $e_strDate != "0000-00-00") { list($eY,$em,$ed) = explode("-",$e_strDate); return( $em.$ed < $m.$d ? $eY-$Y-1 : $eY-$Y ); } else return( date("md") < $m.$d ? date("Y")-$Y-1 : date("Y")-$Y ); } else return "???"; } Robbie

Snippets Manager replied on Tue, 2008/04/01 - 2:22pm

A much easier way to do this is just to compare date("md") against concatenated variables: ($month.$day): function getAge( $p_strDate ) { list($Y,$m,$d) = explode("-",$p_strDate); $years = date("Y") - $Y; if( date("md") < $m.$d ) { $years--; } return $years; } I posted the above because many people don't like ?: syntax...but personally, I like getting things compact, so I would actually do the function like so: function getAge( $p_strDate ) { list($Y,$m,$d) = explode("-",$p_strDate); return( date("md") < $m.$d ? date("Y")-$Y-1 : date("Y")-$Y ); }

Snippets Manager replied on Sun, 2006/07/02 - 7:34pm

I think there is a little bug in the code in that the date checking is not thorough enough, This extra line checks against the day only if the month matches and seems to always give the right answer, function birthday ($birthday) { list($year,$month,$day) = explode("-",$birthday); $year_diff = date("Y") - $year; $month_diff = date("m") - $month; $day_diff = date("d") - $day; if ($month_diff < 0) $year_diff--; elseif (($month_diff==0) && ($day_diff < 0)) $year_diff--; return $year_diff; } echo birthday("1980-07-02"); } also, as an aside, I am very new to php programming and have not posted before, so if I am stepping on peoples toes, I am very sorry :)