Date format ‘xx time ago’

This item was filled under [ Code Snippet, Javascript, PHP ]

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 :mrgreen:

Rate this topic:
1 Star2 Stars3 Stars4 Stars5 Stars (5 votes, average: 5.00 out of 5)
Loading ... Loading ...
Popularity: 3,283 views
Tagged with: [ , , , , , , ]
You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Artikel Sejenis

One Comment on “Date format ‘xx time ago’”

  • Google Chrome Google Chrome 7.0.517.44
    Windows Windows 7
    26 Nov 2010 8:47

    Terimakasih atas informasinya mas.

    Like or Dislike: Thumb up 0 Thumb down 0

Leave a Comment

Notify me of followup comments via e-mail. You can also subscribe without commenting.

Halaman ini di eksekusi dalam waktu 1.272 detik! (koneksi mayan bagus nih...)