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 ?