简体   繁体   中英

Calculating age from date of birth in PHP

What's the most precise function you have come across to work out an age from the users date of birth. I have the following code and was wondering how it could be improved as it doesn't support all date formats and not sure if it's the most accurate function either (DateTime compliance would be nice).

function getAge($birthday) {
    return floor((strtotime(date('d-m-Y')) - strtotime($date))/(60*60*24*365.2421896));
}
$birthday = new DateTime($birthday);
$interval = $birthday->diff(new DateTime);
echo $interval->y;

Should work

Check this

<?php
$c= date('Y');
$y= date('Y',strtotime('1988-12-29'));
echo $c-$y;
?>

Use this code to have full age including years, months and days-

    <?php
     //full age calulator
     $bday = new DateTime('02.08.1991');//dd.mm.yyyy
     $today = new DateTime('00:00:00'); // Current date
     $diff = $today->diff($bday);
     printf('%d years, %d month, %d days', $diff->y, $diff->m, $diff->d);
    ?>

Try using DateTime for this:

$now      = new DateTime();
$birthday = new DateTime('1973-04-18 09:48:00');
$interval = $now->diff($birthday);
echo $interval->format('%y years'); // 39 years

See it in action

This works:

<?
$date = date_create('1984-10-26');
$interval = $date->diff(new DateTime);
echo $interval->y;
?>

If you tell me in what format your $birthday variable comes I will give you exact solution

$date更改为$birthday

WTF?

strtotime(date('dm-Y'))

So you generate a date string from the current timestamp, then convert the date string back into a timestamp?

BTW, one of the reasons it's not working is that strtotime() assumes numeric dates to be in the format m/d/y (ie the US format of date first). Another reason is that the parameter ($birthday) is not used in the formula.

For supper accuracy you need to account for the leap year factor:

function get_age($dob_day,$dob_month,$dob_year){
    $year   = gmdate('Y');
    $month  = gmdate('m');
    $day    = gmdate('d');
     //seconds in a day = 86400
    $days_in_between = (mktime(0,0,0,$month,$day,$year) - mktime(0,0,0,$dob_month,$dob_day,$dob_year))/86400;
    $age_float = $days_in_between / 365.242199; // Account for leap year
    $age = (int)($age_float); // Remove decimal places without rounding up once number is + .5
    return $age;
}

So use:

echo get_date(31,01,1985);

or whatever...

NB To see your EXACT age to the decimal

return $age_float

instead.

This function works fine.

function age($birthday){
 list($day,$month,$year) = 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--;}
 if ($day_diff < 0 && $month_diff < 0){$year_diff--;}
 return $year_diff;
}

See BLOG Post

Here is my long/detailed version (you can make it shorter if you want):

$timestamp_birthdate = mktime(9, 0, 0, $birthdate_month, $birthdate_day, $birthdate_year);
$timestamp_now = time();
$difference_seconds = $timestamp_now-$timestamp_birthdate;
$difference_minutes = $difference_seconds/60;
$difference_hours = $difference_minutes/60;
$difference_days = $difference_hours/24;
$difference_years = $difference_days/365;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM