I am trying to convert a timestamp of the format 2009-09-12 20:57:19
and turn it into something like 3 minutes ago
with PHP.
I found a useful script to do this, but I think it's looking for a different format to be used as the time variable. The script I'm wanting to modify to work with this format is:
function _ago($tm,$rcs = 0) {
$cur_tm = time();
$dif = $cur_tm-$tm;
$pds = array('second','minute','hour','day','week','month','year','decade');
$lngh = array(1,60,3600,86400,604800,2630880,31570560,315705600);
for($v = sizeof($lngh)-1; ($v >= 0)&&(($no = $dif/$lngh[$v])<=1); $v--); if($v < 0) $v = 0; $_tm = $cur_tm-($dif%$lngh[$v]);
$no = floor($no);
if($no <> 1)
$pds[$v] .='s';
$x = sprintf("%d %s ",$no,$pds[$v]);
if(($rcs == 1)&&($v >= 1)&&(($cur_tm-$_tm) > 0))
$x .= time_ago($_tm);
return $x;
}
I think on those first few lines the script is trying to do something that looks like this (different date format math):
$dif = 1252809479 - 2009-09-12 20:57:19;
How would I go about converting my timestamp into that (unix?) format?
Yo can use:
This is actually a better solution I've found. Uses jQuery however it works perfectly. Also it refreshes automatically similar to the way SO and Facebook does so you don't have to refresh the page to see the updates.
This plugin will read your
datetime
attr in the<time>
tag and fill it in for you.http://timeago.yarp.com/
I found results like the following ugly:
Because of that I realized a function that respects plurals, removes empty values and optionally it is possible to shorten the output:
Looks much better:
Optionally use
$level = 2
to shorten it as follows:Remove the
$lang
part if you need it only in English or edit this translation to fit your needs:Slightly modified answer from above:
Use of:
Function: