Finding difference in two timestamp
<p>I am trying to display how much time is gone for the comment has been posted. I am confused now i am trying many way but does not seems to work.</p>

<p>My code just giving <strong>years ago</strong> as output.</p>

<p>here is my code:</p>

<pre><code>$date = new DateTime();
$newtime = $date-&gt;format('U');
$oldtime = $row['time'];
$diffrence = $newtime - $oldtime;
$date = new DateTime("@$diffrence");
$sect = round($date-&gt;format('s'));
$mint = round($date-&gt;format('i'));
$hout = round($date-&gt;format('H'));
$dayt = round($date-&gt;format('d'));
$mont = round($date-&gt;format('m'));
$yeat = round($date-&gt;format('Y'));

if ($yeat &gt; 1970){
    $disptime = "years ago";
} else if ($yeat &lt; 1970) {
    $disptime = "$mont months ago";
} else if ($mont &lt; 0) {
    $disptime = "$dayt days ago";
} else if ($dayt &lt; 0) {
    $disptime = "$hout hours $mint min ago";
} else if ($hout &lt; 0) {
    $disptime = "$mint min $sect sec ago";
} else {
    $disptime = "$sect sec ago";
}
</code></pre>

<p><strong>Edited</strong> Getting near to solution after changing it yeat 0 to 1970</p>

<p><strong>Solved but other way finding tutorial and making my own code here is solved</strong></p>

<pre><code>$start_date = new DateTime($row['timestamp']);
$since_start = $start_date-&gt;diff(new DateTime($date-&gt;format('Y-m-d H:i:s')));
$yeat = $since_start-&gt;y;
$mont = $since_start-&gt;m;
$dayt = $since_start-&gt;d;
$hout = $since_start-&gt;h;
$mint = $since_start-&gt;i;
$sect = $since_start-&gt;s; 

if ($mint == 0 &amp;&amp; $hout == 0 &amp;&amp; $dayt == 0 &amp;&amp; $mont == 0 &amp;&amp; $yeat == 0){
    $disptime = "$sect sec ago";
} else if ($hout == 0 &amp;&amp; $dayt == 0 &amp;&amp; $mont == 0 &amp;&amp; $yeat == 0) {
    $disptime = "$mint min ago";
} else if ($dayt == 0 &amp;&amp; $mont == 0 &amp;&amp; $yeat == 0) {
    $disptime = "$hout hours ago";
} else if ($mont == 0 &amp;&amp; $yeat == 0) {
    $disptime = "$dayt days ago";
} else if ($yeat == 0) {
    $disptime = "$mont months ago";
} else {
    $disptime = "$yeat years ago";
}
</code></pre>
