如何使用Zend_Date计算从日期开始的年龄

时间:2011-08-07 16:41:22

标签: php date zend-framework

如何使用Zend_Date对象计算出生日期的年龄?

7 个答案:

答案 0 :(得分:7)

这是另一种计算年龄的方法。它使用PHP格式。它只是减去年份,然后检查今年的生日是否过去了。这可以像使用标准PHP函数一样容易,但我决定像你问的那样使用Zend_Date。

更新了答案

    Zend_Date::setOptions(array(
        'format_type' => 'php'
    ));
    $today           = new Zend_Date();
    $dateOfBirth     = new Zend_Date('1979-04-10', 'Y-m-d');

    $age             = $today->toString('Y') - $dateOfBirth->toString('Y');
    // Edit: Added julian date adjustments for leap years
    $adjustJulian    = strcmp($today->toString('L'), $dateOfBirth->toString('L'));
    if ($dateOfBirth->toString('m') > 2) {
        $dateOfBirth->addDay($adjustJulian);
    }

    if ($today->toString('z') < $dateOfBirth->toString('z')) {
        $age--;
    }

感谢@maciej-homziuk指出前一个答案的闰年限制。我已经更新了这个答案,为闰年的朱利安日添加了调整。

答案 1 :(得分:1)

我建议你使用这个功能:

public function getAge($dob)
{
    $today = new Zend_Date();
    $dob   = new Zend_Date($dob, Tuffle_Date::getDBFormat());

    $y = Zend_Date::YEAR;
    $m = Zend_Date::MONTH;
    $d = Zend_Date::DAY;

    $dobCurrentYear = new Zend_Date($today->get($y) . '-' . $dob->get($m) . '-' . $dob->get($d), Tuffle_Date::getDBFormat());
    $dobCurrentYear->setYear($today->get('Y'));

    $age = $today->get($y) - $dob->get($y);

    if (!$dobCurrentYear->isLater($today)) {
        $age--;
    }

    return $age;
}

答案 2 :(得分:0)

这是一个用于从zend日期计算年龄的类(请参阅第1个回复): http://forums.zend.com/viewtopic.php?f=69&t=6932

答案 3 :(得分:0)

从这篇文章:how to calculate a difference between two Zend_Date objects, in months

如果我正确阅读了文档,则没有实现两个日期之间的差异,以秒/分钟/.../个月/年为单位,因此您需要自己计算。这样的事情会发生(dunno,如果需要闰年,DST等等):

<?php
$birth = new Zend_Date('1988-25-06');    
$today = new Zend_Date();
$diff = $today->sub($birth)->toValue();
$age = floor($diff / 3600 / 24 / 365);

答案 4 :(得分:0)

这是Zend_Date的做法,但似乎工作正常:

$birthdate = "1975-04-14";<br />
$age = floor((time() - strtotime($birthdate)) / 31556926);

echo "You are " . $age . " years old today.";

答案 5 :(得分:0)

$today = new Zend_Date();
$dateOfBirth = new Zend_Date('1988-25-06');
$age = $today->toString('Y') - $dateOfBirth->toString('Y');
if ($today->toString('dd') + $today->toString('MM') * 32 <
    $dateOfBirth->toString('dd') + $dateOfBirth->toString('MM') * 32)
    $age--;

答案 6 :(得分:0)

我非常喜欢brady.vitrano的方法,它也是建立我自己的行动助手计算年龄的基础,但是他的代码没有考虑闰年(如果出生日期是1979-03-09 [fe] [共同的一年]今天是2012-03-08 [闰年],他的方法返回33,虽然实际年龄是32) - 这是如何在涉及闰年时必须延长以恢复正确的年龄:

Zend_Date::setOptions(array(
    'format_type' => 'php'
));

$today = new Zend_Date();
$dateOfBirth = new Zend_Date($date);

/*** Considering the leap years if the birth date is on 1st march or later. ***/
if ($dateOfBirth->toString('n') > 2) {
    $birthLeap = $dateOfBirth->toString('L');
    $todayLeap = $today->toString('L');
}

$age = $today->toString('Y') - $dateOfBirth->toString('Y');

if ($today->toString('z') - $todayLeap < $dateOfBirth->toString('z') - $birthLeap) {
    $age--;
}

如果您想使用Zend的默认日期说明符(而不是PHP的日期说明符),这就是代码:

$today = new Zend_Date();
$dateOfBirth = new Zend_Date($date);

/*** Considering the leap years if the birth date is on 1st march or later. ***/
if ($dateOfBirth->get('M') > 2) {
    /*** Checking if the birth's year is a leap year. ***/
    if ($dateOfBirth->get(Zend_Date::LEAPYEAR)) {
        $birthLeap = 1;
    }

    /*** Checking if today's year is a leap year. ***/
    if ($today->get(Zend_Date::LEAPYEAR)) {
        $todayLeap = 1;
    }
}

$age = $today->get('Y') - $dateOfBirth->get('Y');

if ($today->get('D') - $todayLeap < $dateOfBirth->get('D') - $birthLeap) {
    $age--;
}