Date format ‘xx time ago’

This item was filled under [ Code Snippet, 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++)
	$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

Semoga membantu :mrgreen:

Rate this topic:
1 Star2 Stars3 Stars4 Stars5 Stars (2 votes, average: 5 out of 5)
Loading ... Loading ...
Popularity: 823 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

Leave a Comment

Halaman ini di eksekusi dalam waktu 13.22 detik! (waaaaaaaaaaa mulai lambat neehh...)