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
Calculate Age
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
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
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
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 yearSnippets Manager replied on Wed, 2009/09/23 - 6:32am
Snippets Manager replied on Fri, 2008/12/26 - 10:12pm
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
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
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
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 "???"; }RobbieSnippets Manager replied on Tue, 2008/04/01 - 2:22pm
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
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 :)