0

i've got an age calc script for PHP. But, it seems to work with some dates, i don't get why it wont work.

function Agecalc($birthday) 
{

    list($day,$month,$year) = explode("/",$birthday);
    $day_diff   = date("d") - $day;
    $month_diff = date("m") - $month;
    $year_diff  = date("Y") - $year;
    if ($day_diff < 0 || $month_diff < 0)
        $year_diff--;
    return $year_diff;

}

echo Agecalc('19/2/1994');

This returns "17" when it should be 18?

Ninja__1881
  • 51
  • 2
  • 4

6 Answers6

1

Your function returns wrong age at each time input day part (in input_date) is superior to current day part (in current_date).

Below please kindly a function that could help. For any issue, please let me know.

/* By default, 
* format is 'us' 
* and delimiter is '-'
*/

function date_calculate($input_date, $format = 'us', $delimiter = '-')
{
    switch (strtolower($format)) {
        case 'us': // date in 'us' format (yyyy/mm/dd), like '1994/03/01'
            list($y, $m, $d) = explode($delimiter, $input_date);
            break;
        case 'fr': // date in 'fr' format (dd/mm/yyyy), like '01/03/1994'
            list($d, $m, $y) = explode($delimiter, $input_date);
            break;
        default: return null;
    }

    $tz          = new \DateTimeZone('UTC'); // TimeZone. Not required but can be useful. By default, server TimeZone will be returned
    $format_date = sprintf('%s-%s-%s', $y, $m, $d);
    $cur_date    = new \DateTime(null, $tz);
    $user_date   = new \DateTime($format_date, $tz);
    $interval    = $user_date->diff($cur_date);

    return [
        'day'   => $interval->format('%r%d'),
        'month' => $interval->format('%r%m'),
        'year'  => $interval->format('%r%y'),
    ];
}

var_dump(date_calculate('19/02/1994', 'fr', '/'));

More++:

1

Aside from the fact you should use the built in functions for date diff, lets address the issue of why your presented code doesn't work as you expect by breaking it down.

function Agecalc($birthday) 
{
    list($day,$month,$year) = explode("/",$birthday);
    $day_diff   = date("d") - $day; // 9 - 19 = -10
    $month_diff = date("m") - $month; // 3 - 2 = 1
    $year_diff  = date("Y") - $year; // 2012 - 1994 = 18

Now what you're doing in your original code is saying if the day diff or the month diff is negative, then the age is out by a whole year. This is fundamentally flawed, since when the day diff is negative the calculation is potentially out by only a month, not a year.

So you cater for the negative days by decrementing the month difference.

    if ($day_diff < 0) {
        $month_diff--;
    }

Now the day diff is taken care of and our month diff = 0. If it was negative we'd still need to account for it, so leave this block at the end.

    if ($month_diff < 0) {
        $year_diff--;
    }

    return $year_diff;
}

The final result from your example now returns 18, because even after accounting for the negative days, the months are still positive.

Leigh
  • 12,859
  • 3
  • 39
  • 60
0

This is more short simple php to calculate ages from birthday

function getAge($birthdate){
  return floor((time() - strtotime($birthdate))/31556926);
}
richcode
  • 1
  • 1
0

It is going to be beneficial to use PHP's built in DateTime functions to calculate this, specifically DateTime::diff(..).

function AgeCalc($birthday)
{
    $dateBirthday = DateTime::createFromFormat('d/m/Y', $birthday);
    $dateNow = new DateTime();
    $difference = $dateNow->diff($dateBirthday);
    return $difference->y;
}
Rudi Visser
  • 21,350
  • 5
  • 71
  • 97
0

You shouldn't be testing for day_diff<0 unless you've established that it's the same month. Currently it subtracts a year if it's the 5th of December and my birthday is the 13th of April, because 13 is bigger than 5.

octern
  • 4,825
  • 21
  • 38
0

UPDATE

I made a blog post about the 3 most popular funcs to get age in PHP.

See the results

HERE

OLD

In One Line: FYI, there is much shorter way that's comparable to how we Humans calculate birthdays:

return intval(substr(date('Ymd') - date('Ymd', strtotime($birthday)), 0, -4))
SpYk3HH
  • 22,272
  • 11
  • 70
  • 81