php - Getting time eg 5 minutes ago -
this question has answer here:
i have added timestamp database have exact time post made , '5 minutes ago' or long has been.
is there php method or or how it?
thanks
there no native function this, have write script... works me:
php
function elapsedtime($time_since) { $time = time() - $time_since; $tokens = array ( 31536000 => 'year', 2592000 => 'month', 604800 => 'week', 86400 => 'day', 3600 => 'hour', 60 => 'minute', 1 => 'second' ); foreach ($tokens $unit => $text) { if ($time < $unit) continue; $numberofunits = floor($time / $unit); return $numberofunits.' '.$text.(($numberofunits>1)?'s':''); } } }
html
<div class="answertime"> asked <?= elapsedtime($time_since) /* time variable */ ?> ago </div>
Comments
Post a Comment