Skip to content

Instantly share code, notes, and snippets.

@alemohamad
Created December 14, 2013 19:36
Show Gist options
  • Save alemohamad/7963826 to your computer and use it in GitHub Desktop.
Save alemohamad/7963826 to your computer and use it in GitHub Desktop.
Time Ago Function
<?php
// This can be used for comments and other from of communication to tell the time ago
// instead of the exact time which might not be correct to some one in another time zone.
// The function only uses unix time stamp like the result of time();
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment