Php 如何获得所有单位(y、m、d、h、i、s)中两个日期时间之间的差异?

Php 如何获得所有单位(y、m、d、h、i、s)中两个日期时间之间的差异?,php,datetime,Php,Datetime,我想得到两个datetime之间的差值,因此如果差值仅为秒,我想只回显秒,依此类推,例如,输出应该是这样的: 5秒前 5分钟前 5小时前 这是我使用的代码,但它只给我天数: $date1 = new DateTime(date('Y-m-d', strtotime("2013-08-07 13:00:00"))); $date2 = new DateTime(date('Y-m-d', strtotime("2012-08-08 12:00:00"))); echo $date1->di

我想得到两个datetime之间的差值,因此如果差值仅为秒,我想只回显秒,依此类推,例如,输出应该是这样的:

5秒前

5分钟前

5小时前

这是我使用的代码,但它只给我天数:

$date1 = new DateTime(date('Y-m-d', strtotime("2013-08-07 13:00:00")));
$date2 = new DateTime(date('Y-m-d', strtotime("2012-08-08 12:00:00")));
echo $date1->diff($date2)->days;
这段代码给了我想要的东西,但同时:

$x = new DateTime($career->postdate);
$interval = $x->diff(new DateTime(date('Y-m-d H:i:s')));
$elapsed = $interval->format('%y years %m months %a days %h hours %i minutes %S seconds');
echo $elapsed;

我知道我可以在第二个代码中使用ouput字符串来实现我的目标,但这不是首选的方式,所以我如何才能以最佳方式实现这一点

我就是这样通过处理format()函数的输出来解决的:

                            $x = new DateTime($career->postdate);
                            $interval = $x->diff(new DateTime(date('Y-m-d H:i:s')));
                            $elapsed = $interval->format('%y years %m months %a days %h hours %i minutes %S seconds');
                            if($interval->format('%y') !=0){

                                if($interval->format('%m') !=0){

                                    echo $interval->format('%y years %m months ago.');
                                }
                                else{

                                    echo $interval->format('%y years ago.');
                                }       
                            }

                            elseif($interval->format('%m') !=0){

                                if($interval->format('%a') !=0){

                                    echo $interval->format('%m months %a days ago.');
                                }
                                else{

                                    echo $interval->format('%m months ago.');
                                }  
                            }

                            elseif($interval->format('%d') !=0){

                                if($interval->format('%h') !=0){

                                    echo $interval->format('%a days %h hours ago.');
                                }
                                else{

                                    echo $interval->format('%a days ago.');
                                }  
                            }

                            elseif($interval->format('%h') !=0){

                                if($interval->format('%i') !=0){

                                    echo $interval->format('%h hours %i minutes ago.');
                                }
                                else{

                                    echo $interval->format('%h hours ago.');
                                }  
                            }

                            elseif($interval->format('%i') !=0){

                                if($interval->format('%S') !=0){

                                    echo $interval->format('%i minutes %S seconds ago.');
                                }
                                else{

                                    echo $interval->format('%i minutes ago.');
                                }  
                            }

                            elseif($interval->format('%S') !=0){

                                echo $interval->format('%S seconds ago.'); 
                            }
function x($i, $s) {
    return ($i>0 ? $i.$s : "");
}   

function f($int) {
    $ret = x($int->y, 'y ');
    $ret.= x($int->m, 'm ');
    $ret.= x($int->d, 'd ');
    $ret.= x($int->h, 'h ');
    $ret.= x($int->i, 'min ');
    $ret.= x($int->s, 's ');
    return $ret;
}

$date1 = new DateTime("2013-08-08 13:00:00");
$date2 = new DateTime("2012-08-06 12:30:00");
$interval = $date1->diff($date2);
echo f($interval);