0

我试图从 ExpressionEngine 网站上的出生日期计算一个人的年龄。以下代码适用于我的本地测试站点,但服务器使用的是旧版本的 PHP (5.2.17),所以我遇到了错误。有人可以建议我需要使用什么代码吗?

{exp:channel:entries channel='zoo_visitor'}
<?php
$dob = new DateTime('{member_birthday format='%Y-%m-%d'}');
$now = new DateTime('now');
// This returns a DateInterval object.
$age = $now->diff($dob);
// You can output the date difference however you choose.
echo 'This person is ' .$age->format('%y') .' years old.';
?>
{/exp:channel:entries}
4

7 回答 7

3

您当前的代码将无法工作,因为DateTime::diff它是在 PHP 5.3.0 中引入的。

通常,日期算术非常棘手,因为您必须考虑时区、DST 和闰年,但对于像计算“全年”差异这样简单的任务,您可以很容易地做到这一点。

这个想法是结果等于结束日期的年份减去开始日期的年份,如果开始日期的月/日在一年内早于结束日期,则应从中减去 1。编码:

$dob = new DateTime('24 June 1940');
$now = new DateTime('now');

echo year_diff($now, $dob);

function year_diff($date1, $date2) {
    list($year1, $dayOfYear1) = explode(' ', $date1->format('Y z'));
    list($year2, $dayOfYear2) = explode(' ', $date2->format('Y z'));
    return $year1 - $year2 - ($dayOfYear1 < $dayOfYear2);
}

看到它在行动。请注意结果如何在指定生日的同一天增加 1。

于 2012-06-27T09:31:41.967 回答
2
$dob = strtotime('{member_birthday format='%Y-%m-%d'}');
$now = time();
echo 'This person is ' . (1970 - date('Y', ($now - $dob))) .' years old.';
于 2012-06-27T09:24:38.647 回答
1

您只能使用PHP 5.3 以上的差异

您可以尝试修改,它适用于 5.2

$age = $now->modify('-' . $dob->format('Y') . 'year');
于 2012-06-27T09:23:07.340 回答
1

经过大量搜索,我找到了答案:

<?php
    //date in mm/dd/yyyy format
    $birthDate = "{member_birthday format='%m/%d/%Y'}";
    //explode the date to get month, day and year
    $birthDate = explode("/", $birthDate);
    //get age from date or birthdate
    $age = (date("md", 
                 date("U", 
                      mktime(0, 
                             0, 
                             0, 
                             $birthDate[0], 
                             $birthDate[1], 
                             $birthDate[2])
                     )
                 )
            > date("md") 
            ? ((date("Y") - $birthDate[2]) - 1)
            : (date("Y") - $birthDate[2]));
    echo $age;
?>   
于 2012-06-28T13:05:44.927 回答
0

在某些极端情况下,仅使用一年中的某一天的计算会减少一个:它们显示 2012-02-29 和 2011-03-01 为 1 年,而这应该是 0 年(以及 11 个月和 28 天)。考虑闰年的可能解决方案是:

<?php
    function calculateAge(DateTime $birthDate, DateTime $now = null) {
        if ($now == null) {
            $now = new DateTime;
        }

        $age = $now->format('Y') - $birthDate->format('Y');
        $dm = $now->format('m') - $birthDate->format('m');
        $dd = $now->format('d') - $birthDate->format('d');

        if ($dm < 0 || ($dm == 0 && $dd < 0)) {
            $age--;
        }

        return $age;
    }

    echo calculateAge(new DateTime('2011-04-01'), new DateTime('2012-03-29'));

不过,user579984 的解决方案也有效。

于 2013-01-18T10:35:50.240 回答
0
$birthday = '1983-03-25';
$cm = date('Y', strtotime($birthday));
$cd = date('Y', strtotime('now'));

$res = $cd - $cm;

if (date('m', strtotime($birthday)) > date('m', strtotime('now')))
    $res--;
else if ((date('m', strtotime($birthday)) == date('m', strtotime('now'))) &&
    (date('d', strtotime($birthday)) > date('d', strtotime('now'))))
    $res--;

echo $res;
于 2013-01-18T12:51:42.330 回答
0

我一直在使用它,它从未让我失望。YYYYMMDD是这个人的生日。

$age = floor((date('Ymd') - 'YYYYMMDD') / 10000);

如果您想更严格,您可以使用intval函数将日期转换为整数,或在日期前加上(int).

于 2018-11-29T09:30:19.893 回答