Kita sering melihat pada website menggunakan format “XX times ago”, alternatif dari penampilan tanggal.
Berikut adalah fungsi dan cara penggunaan-nya:
<?php
// FUNGSI
function ago($timestamp){
$difference = time() - $timestamp;
$periods = array("second", "minute", "hour", "day", "week", "month", "years", "decade");
$lengths = array("60","60","24","7","4.35","12","10");
for($j = 0; $difference >= $lengths[$j]; $j++) {
if(isset($lengths[$j]) && $lengths[$j] > 0) {
$difference /= $lengths[$j];
}
}
$difference = round($difference);
if($difference != 1) $periods[$j].= "s";
$text = "$difference $periods[$j] ago";
return $text;
}
// PENGGUNAAN
$tanggal = strtotime("2009-06-01 20:22:00");
echo ago($tanggal); // => sekarang adalah 18 juli 2009, maka hasilnya: "2 months ago"
?>
sumber: http://drupal.org/node/61565
Catatan: Untuk implementasi, lebih baik menggunakan sisi client, yaitu menggunakan Javascript dari jQuery Plugin berikut ini: link nya http://timeago.yarp.com/
Kutipan dari web tersebut:
Isikan script pada head HTML:
<script src="jquery.min.js" type="text/javascript"></script>
<script src="jquery.timeago.js" type="text/javascript"></script>
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery("abbr.timeago").timeago();
});
</script>
Isi kan pada body HTML seperti ini, contoh:
<abbr class="timeago" title="2008-07-17T09:24:17Z">July 17, 2008</abbr>
Download Plugin Times Ago disini
Download jQuery disini
Semoga bermanfaat

Loading ...
Popularity: 3,283 views