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?
I modified the original function a bit to be (in my opinion more useful, or logical).
This is what I went with. Its a modified version of Abbbas khan's post:
i made this and it's working just fine it's working for both unix timestamp like
1470919932
or formatted time like16-08-11 14:53:30
Originally written and available at this link
Needs a time() value, and it will tell you how many seconds/minutes/hours/days/years/decades ago.
If using MySQL, use the
UNIX_TIMESTAMP()
function.