it's not a cool idea to call date() 3 times, in case you have >20 calls of such function it may become significant.
function age($birthday){ $now = time(); $dt=$now-$birthday; //let us check if current the date is far enought from birthday //(>2days), we can use simlified way to calculate age if( !((($dt+172800)%31557600)<345600)){ $age=floor($dt/31557600); }else{ $datestamp = date("d.m.Y", $now); list($current_day, $current_month,$current_year)= explode("." , $datestamp); list($birth_year, $birth_month, $birth_day) = explode("-" , date('Y-m-d',$birthday));
Original snippet does not work for all dates and neither does the snippet with the first comment.
Snippet with second comment is just wrong in so many ways!.
I have attached my version of the function which should work for all dates and it only calls the date function once.
<?php $dob = "1980-11-12"; echo("Age is: " . age($dob));
function age($dob){ // get todays date in same format as dob $today = date("Y-m-d"); // split dob into year month day list($year, $month, $day) = explode("-", $dob); // split todays date into year month day list($this_year, $this_month, $this_day) = explode("-", $today); // Calculate the difference in years $age = $this_year - $year; // If current month is before dob month, then minus 1 year if($this_month < $month){ $age--; } // If current month same as dob month AND current day is before // dob day then minus 1 year elseif(($this_month == $month) && ($this_day < $day)){ $age--; }
Here is some updated code for you:
--
Chris Gmyr
www.chrisgmyr.com
www.syracusecs.com
www.syracusebands.net
<?php
$age = "1985-03-13";
echo age($age);
function age($age){
list($year,$month,$day) = explode("-",$age);
$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--;
elseif (($month_diff==0) && ($day_diff >= 0))$year_diff++;
return $year_diff;
}
?>
function age($birthday){
$now = time();
$dt=$now-$birthday;
//let us check if current the date is far enought from birthday
//(>2days), we can use simlified way to calculate age
if( !((($dt+172800)%31557600)<345600) ){
$age=floor( $dt/31557600 );
}else{
$datestamp = date("d.m.Y", $now);
list($current_day, $current_month,$current_year)= explode("." , $datestamp);
list($birth_year, $birth_month, $birth_day) = explode("-" , date('Y-m-d',$birthday));
$year_dif = $current_year - $birth_year;
if(($birth_month > $current_month) || ($birth_month == $current_month && $current_day < $birth_day)){
$age = $year_dif - 1;
}else{
$age = $year_dif;
}
}
return $age;
}
Snippet with second comment is just wrong in so many ways!.
I have attached my version of the function which should work for all dates and it only calls the date function once.
<?php
$dob = "1980-11-12";
echo("Age is: " . age($dob));
function age($dob) {
// get todays date in same format as dob
$today = date("Y-m-d");
// split dob into year month day
list($year, $month, $day) = explode("-", $dob);
// split todays date into year month day
list($this_year, $this_month, $this_day) = explode("-", $today);
// Calculate the difference in years
$age = $this_year - $year;
// If current month is before dob month, then minus 1 year
if ($this_month < $month) {
$age--;
}
// If current month same as dob month AND current day is before
// dob day then minus 1 year
else if (($this_month == $month) && ($this_day < $day)) {
$age--;
}
return $age;
}
?>