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 tried this and works fine for me
Check this link for reference here
Thanks! and have fun.
I'm aware that there are several answers here, but this is what I came up with. This only handles MySQL DATETIME values as per the original question I was responding to. The array $a needs some work. I welcome comments on how to improve. Call as:
echo time_elapsed_string('2014-11-14 09:42:28');
You'll have to take each individual piece of your timestamp, and convert it into Unix time. For example for the timestamp, 2009-09-12 20:57:19.
(((2008-1970)*365)+(8*30)+12)*24+20 would give you a ROUGH estimate of the hours since January 1st, 1970.
Take that number, multiply by 60 and add 57 to get the minutes.
Take that, multiply by 60 and add 19.
That would convert it very roughly and inaccurately however.
Is there any reason you can't just take the normal Unix time to begin with?
I am using following function for several years. And it is working fine:
This function is not made to be used for the English language. I translated the words in English. This needs more fixing before using for English.
The following is a very simple and extremely efficient solution.
echo timeElapsed(1201570814);
Sample output:
6yrs 4months 3weeks 4days 12hrs 40mins and 36secs.