开发者

PHP calculate person's current age

开发者 https://www.devze.com 2023-01-09 23:03 出处:网络
I have birth dates on my site in format 12.01.1980. $person_date (string) = Day.Month.Year Want to add an oldness of the person. Like \"Currently 30 years\" (2010 - 1980 = 30 years).

I have birth dates on my site in format 12.01.1980.

$person_date (string) = Day.Month.Year

Want to add an oldness of the person. Like "Currently 30 years" (2010 - 1980 = 30 years).

But makin the function just on years can't give the perfect result:

If person birth date is 12.12.1980 and current date is 01.01.2010 the person doesn't have 30 years old. It's a 29 years and one month.

There must be a calculation on targeting both year, month and day of birt开发者_如何转开发h with comparison of current date:

0) Parse the dates.

Birth date (Day.Month.Year):
Day = $birth_day;
Month = $birth_month;
Year = $birth_year;

Current date (Day.Month.Year):
Day = $current_day;
Month = $current_month;
Year = $current_year;

1) year comparison, 2010 - 1980 = write "30" (let it be $total_year variable)

2) compare the months, if (birth date month is bigger > than current month (like 12 in birth and 01 current)) { do minus one year from $total_year variable (30 - 1 = 29) }. If do minus happened, finish the calculations at this point. Else go the next (3 step).

3) else if (birth month < current month) { $total_year = $total_year (30); }

4) else if (birth month = current month) { $total_year = $total_year (30); }

and check the day (in this step):

 if(birth day = current day) { $total_year = $total_year; }
 else if (birth day > current day) { $total_year = $total_year -1; }
 else if (birth day < current day) { $total_year = $total_year; }

5) echo $total_year;

My php knowledge isn't good, hope you can help.

Thanks.


You can use the DateTime class and its diff() method.

<?php
$bday = new DateTime('12.12.1980');
// $today = new DateTime('00:00:00'); - use this for the current date
$today = new DateTime('2010-08-01 00:00:00'); // for testing purposes

$diff = $today->diff($bday);

printf('%d years, %d month, %d days', $diff->y, $diff->m, $diff->d);

prints 29 years, 7 month, 20 days


An extension of @VolkerK's answer - which is excellent! I never like seeing the age of zero, which happens if you only use the year. This function shows their age in months (if they are one month old or more), and otherwise in days.

function calculate_age($birthday)
{
    $today = new DateTime();
    $diff = $today->diff(new DateTime($birthday));

    if ($diff->y)
    {
        return $diff->y . ' years';
    }
    elseif ($diff->m)
    {
        return $diff->m . ' months';
    }
    else
    {
        return $diff->d . ' days';
    }
}


I've further extended @Jonathan's answer, to give a more 'human-friendly' response.

Using these dates:

$birthday= new DateTime('2011-11-21');
//Your date of birth.

And calling this function:

function calculate_age($birthday)
{
    $today = new DateTime();
    $diff = $today->diff(new DateTime($birthday));

    if ($diff->y)
    {
        return 'Age: ' . $diff->y . ' years, ' . $diff->m . ' months';
    }
    elseif ($diff->m)
    {
        return 'Age: ' . $diff->m . ' months, ' . $diff->d . ' days';
    }
    else
    {
        return 'Age: ' . $diff->d . ' days old!';
    }
}; 

Is returning:

Age: 1 years, 2 months

Cute - for really young ones only a few days old!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号