如何在所有单位(y,m,d,h,i,s)中得出两个日期时间之间的差异?
I want to get the difference between two datetime, so if the difference is just seconds, I want to echo the seconds only and so on, for example the output should be something like:
5 seconds ago
5 minutes ago
5 hours ago
...
here is my code that I used but it give me the days only:
$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;
and this code is giving me what I want but all at once:
$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;
I know that I can play with the ouput string in the second code to achieve my goal, but its not a preferred way, so how I can do that in the best way ?
我想得到两个日期时间之间的差异,所以如果差异只是几秒钟,我想回应秒 只有等等,例如输出应该是这样的: p>
这是我用过的代码 但它只给我一些日子: p>
这段代码给了我想要的东西但是一次又一次: p>
I 知道我可以在秒中使用输出字符串 ond代码实现了我的目标,但它不是首选的方式,那么我怎么能以最好的方式做到这一点? p>
div> 5秒前 code> p>
5 分钟前 code> p>
5小时前 code>
... p>
$ date1 = new DateTime(date('Ym-d',strtotime(“2013-08-07 13:00:00”) )));
$ date2 = new DateTime(date('Ym-d',strtotime(“2012-08-08 12:00:00”)));
echo $ date1-> diff($ date2) - > days;
code> pre>
$ x = new DateTime($ career-> postdate);
$ interval = $ x-> diff(new DateTime(date('Ymd H:i:s')));
$ elapsed = $ interval - >格式('%y年%%m个月%a天%h小时%i分钟%S秒');
echo $ elapsed;
code> pre>
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);
this is how I solved it by dealing with the output of format() function:
$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.');
}