0

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 ?

Constantin Groß
  • 10,719
  • 4
  • 24
  • 50
MD.MD
  • 708
  • 4
  • 14
  • 34

2 Answers2

2
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);
matepal297
  • 961
  • 1
  • 11
  • 19
0

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.'); 
                            }
MD.MD
  • 708
  • 4
  • 14
  • 34