Skip to content

Instantly share code, notes, and snippets.

@samuraee
Last active August 29, 2015 14:23
Show Gist options
  • Save samuraee/0b83bb62ada94b9f6c56 to your computer and use it in GitHub Desktop.
Save samuraee/0b83bb62ada94b9f6c56 to your computer and use it in GitHub Desktop.
<?php
function ago($tm, $lang = 'fa', $ashtml = true) {
$local = array('style' => array('fa' => 'style="<a href="http://direction:rtl">direction:rtl</a>;"',
'en' => 'style="<a href="http://direction:ltr">direction:ltr</a>"'),
'times' => array(
'fa' => array('ثانیه', 'دقیقه', 'ساعت', 'روز', 'هفته', 'ماه', 'سال', 'دهه'),
'en' => array('second', 'minute', 'hour', 'day', 'week', 'month', 'year', 'decade')),
'ago' => array('fa' => 'پیش', 'en' => 'ago'));
if (intval($tm) > 0) {
$cur_tm = time();
$dif = $cur_tm - $tm;
$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 && $lang == 'en')
$local['times'][$lang][$v] .= 's'; $x = sprintf("%d %s ", $no, $local['times'][$lang][$v]);
if ($ashtml)
return " " . $x . ' ' . $local['ago'][$lang] . "";
else
return $x . ' ' . $local['ago'][$lang];
} else {
return '-';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment