Date format ‘xx time ago’
Posted by Jawaad on 18 July, 2009
No comments yet
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 ![]()
Popularity: 823 views

