php PHP计算人的当前年龄

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3380990/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:32:33  来源:igfitidea点击:

PHP calculate person's current age

phpdate

提问by James

I have birth dates on my site in format 12.01.1980.

我的网站上有格式 的出生日期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).

想加个老字号。像“目前 30 年”(2010 - 1980 = 30 年)。

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

但是仅仅几年的功能并不能给出完美的结果:

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

如果某人的出生日期是12.12.1980,当前日期是01.01.2010该人,则该人没有 30 岁。29岁零一个月了。

There must be a calculation on targeting both year, month and dayof birth with comparison of current date:

必须针对出生年月日进行计算,并与当前日期进行比较:

0) Parse the dates.

0) 解析日期。

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_yearvariable)

1) 年份比较,2010 - 1980 = 写“30”(让它$total_year可变)

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_yearvariable (30 - 1 = 29) }. If do minus happened, finish the calculations at this point. Else go the next (3 step).

2)比较月份,如果(出生日期月份大于当前月份(例如出生日期为 12 和当前月份为 01)){从$total_year变量(30 - 1 = 29)中减去一年}。如果发生减号,此时完成计算。否则进行下一步(3 步)。

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

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

4) 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;

5) 回声 $total_year;

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

我的php知识不是很好,希望能帮到你。

Thanks.

谢谢。

回答by VolkerK

You can use the DateTime classand its diff()method.

您可以使用DateTime 类及其diff()方法。

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

印刷 29 years, 7 month, 20 days

回答by Jonathan

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.

@VolkerK 答案的扩展 - 非常好!我从不喜欢看到零的年龄,如果你只使用年份,就会发生这种情况。此函数以月为单位显示它们的年龄(如果它们是 1 个月或更长时间),否则以天为单位。

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';
    }
}

回答by itsricky

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

我进一步扩展了@Jonathan 的回答,以提供更“人性化”的回应。

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!

可爱 - 对于只有几天大的非常年轻的人!