Forum Moderators: coopster
<?php
function CalculateAge($BirthDate)
{
list($Year, $Month, $Day) = explode("/", $BirthDate);
$YearDiff = date("Y") - $Year;
if(date("m") < $Month || (date("m") == $Month && date("d") < $DayDiff))
{
$YearDiff--;
}
return $YearDiff;
}
echo CalculateAge("1986/06/18");
?>
I didn't know about date_diff before you mentioned it.
<?php
$date = "1987/02/03";
function CalculateAge($date)
{
$y = date('Y');
$m = date('n');
$d = date('j');
list($yr,$mo,$day) = explode('/',$date);
$now = ($y*10000+$m*100+$d);
$past = ($yr*10000+$mo*100+$day);
$diff = ($past-$now);
if ($diff>0) { $age = 0 ; }
else
{
$age = (($y-$yr)-1);
if (($m>$mo) || (($m>=$mo) && ($d>=$day))) { $age++; }
}
return $age;
}
$age = CalculateAge($date);
?> [edited by: CyBerAliEn at 11:56 pm (utc) on Sep 14, 2010]
if(date("m") < $Month || (date("m") == $Month && date("d") < $DayDiff)) if(date("m") < $Month || (date("m") == $Month && date("d") < $Day)) lexipixel's code is still giving me the Undefined offset error on the same line
/**
* Variation of Anyango's script
* @param dob string|int Date string that strtotime() understands OR unix timestamp (quicker)
* @return int
*/
function calcAge_Anyango($dob) {
if (is_string($dob)) {
$dob = strtotime($dob);
}
$age = date("Y") - date("Y",$dob);
if (date("z") < date("z",$dob)) $age--;
return $age;
}
CyBerAliEn: Due to the more lengthy computation of this situation, I would not advise trying to use a database solution. IF all you wanted is a raw difference between 2 dates, then yes, a database approach would be feasible. But in this case of calculating age, no.
I'm not sure that I agree. This could depend on the nature of the query - if you just want to display all records older than a certain age (ie. you've pre calculated the DOB) OR you wish to display everyone's age! If the later, then keeping it all in the database is probably the best approach. It's only a one-liner in the SQL query (see Sub_Seven's post above) and providing you are storing your dates as DATEs in your DB it's probably quicker.
DATEDIFF(expr1,expr2) ((YEAR(CURDATE())-YEAR(date_of_birth)) - (RIGHT(CURDATE(),5)<RIGHT(date_of_birth,5))) AS age SELECT name,date_of_birth,((YEAR(CURDATE())-YEAR(date_of_birth)) - (RIGHT(CURDATE(),5)<RIGHT(date_of_birth,5))) AS age,state FROM people ORDER BY age DESC; name | date_of_birth | age | state
John , 1987-04-21, 23, New York
Bob, 1988-03-02, 22, Ohio
Mike, 1988-01-14, 22, Maine
Pat, 1988-11-24, 21, Utah $age = $result['age']; function getAge($date,$format="yyyymmdd",$delimiter="/")
{
//code
} Well all of a sudden I realized I could just do this:
echo date('Y-m-d') - $result['date_of_birth'];
And I was like whatta hell!? I got the age just like all other three scripts in just one single line?
// Expected age is 29, not 30 until 1st Nov 2010
echo '2010-09-21' - '1980-11-01';
// PHP converts these to numeric values
echo 2010 - 1980;
// Output 30 - Not the age, just the difference in numeric value (which so happens to be years)