4

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));
}
HamZa
  • 14,671
  • 11
  • 54
  • 75

10 Answers10

14
$birthday = new DateTime($birthday);
$interval = $birthday->diff(new DateTime);
echo $interval->y;

Should work

KingCrunch
  • 128,817
  • 21
  • 151
  • 173
  • 1
    "it doesn't" is not an error message. Additional if especially the first line works highly depends on how `$date` looks like. http://php.net/datetime.createfromformat -- Just tested it: Works fine. – KingCrunch Apr 17 '12 at 07:23
  • One catch with this method is: Say for example, if today is 23rd Sept 2014; This will return 26 years for a person born on 20 Sept 1988 but 25 years for a person born on 25 Sept 1988. Even though this is technically correct, that might not be the expected answer. Since we are displaying only the year it makes a huge difference. This is probably why it didn't work for @user955822. It is always good to decide `BOD as on date` before calculating age. – Jayarathina Madharasan Sep 23 '14 at 05:31
  • @JayarathinaMadharasan I disagree. Asking for a persons age refers to his/her age, in which case 26 is wrong, when he/she isn't 26. What I mean: It isn't just technically correct, it is _correct_ correct (that's how calendars work). If there are confusions regarding the year, maybe the presentation is the issue. – KingCrunch Sep 24 '14 at 10:33
  • 1
    If you want age in years, and age increases by 1 on the person's birthday, then this code is correct. – Nik Dow Dec 25 '15 at 11:16
9

Check this

<?php
$c= date('Y');
$y= date('Y',strtotime('1988-12-29'));
echo $c-$y;
?>
Dinithi De Silva
  • 1,142
  • 5
  • 28
  • 46
  • 1
    Would you consider adding some narrative to explain why this code works, and what makes it an answer to the question? This would be very helpful to the person asking the question, and anyone else who comes along. – Andrew Barber Mar 01 '13 at 05:09
  • 1
    I came around, looking for a simple solution and it works, no explanation needed, it's self-explanatory. Thank you for this piece of code. – Kristjan O. Jul 21 '14 at 15:11
  • This won't work for dates outside of the range of 1970 - 2038, will it? That's what `DateTime` is for. – mbomb007 May 26 '21 at 16:20
3

Try using DateTime for this:

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

See it in action

John Conde
  • 217,595
  • 99
  • 455
  • 496
3

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);
    ?>
Mandeep Singh
  • 983
  • 8
  • 9
0

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

Milan Halada
  • 1,943
  • 18
  • 28
0

Change the $date to $birthday.

New in php
  • 175
  • 1
  • 1
  • 3
0

WTF?

strtotime(date('d-m-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 (i.e. the US format of date first). Another reason is that the parameter ($birthday) is not used in the formula.

symcbean
  • 47,736
  • 6
  • 59
  • 94
0

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...

N.B. To see your EXACT age to the decimal

return $age_float

instead.

Luke Watts
  • 457
  • 5
  • 13
0

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

Subin
  • 3,445
  • 1
  • 34
  • 63
0

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;
Pang
  • 9,564
  • 146
  • 81
  • 122
legibe
  • 552
  • 2
  • 6
  • 19