2012-06-27 200 views
0

我正试图从ExpressionEngine网站上的出生日期计算出一个人的年龄。 以下代码适用于我的本地测试站点,但服务器正在使用较早版本的PHP(5.2.17),因此我不知所措。 有人可能会建议我需要使用什么代码吗?计算PHP的年龄5.2

{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} 
+2

你有什么错误? – Adi

回答

1

可以使用diff只以上PHP 5.3

您可以修改尝试,它可以在5.2

$age = $now->modify('-' . $dob->format('Y') . 'year'); 
2
$dob = strtotime('{member_birthday format='%Y-%m-%d'}'); 
$now = time(); 
echo 'This person is ' . (1970 - date('Y', ($now - $dob))) .' years old.'; 
+0

它显示负值 –

+0

@Jon从什么时候一个简单的整数变量有'getTimestamp'方法? – slash197

+0

我试过以下内容:'$ dob = strtotime('{member_birthday format ='%Y-%m-%d'}'); $ now = time();回声(1970年 - 日期('Y',($ dob - $ now)));'但这显示一个年龄比 – user579984

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

See it in action。注意结果如何在生日指定的同一天增加1。

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

只用一年的天的计算是关闭一个在某些极端情况:它们显示1年2012-02-29 2011-2020和03-01,而这应该是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的解决方案工作也一样,虽然。

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;