-->

Membuat Tanggal Seperti Facebook TimeAgo di Yii

Hmm.. mungkin kebanyakan kalau membuat aplikasi PHP untuk menampilkan tanggal biasanya langsung ditampilkan apa adanya seperti 2010-10-10 23:11:12. Nah kali ini ane ingin memberikan sedikit tutorial bagaimana caranya untuk membuat tampilan yang lebih user friendly mungkin seperti Facebook

Status 1
 1 minutes ago
Status 2
 a day ago

Yup untuk membuat tampilan seperti itu kita pertama kali harus tentuin dulu pastiin database yang kita pakai di table kita type data TIMESTAMP bukan date,datetime atau yang lainnya.

Okeh langsung saja pertama kali kita harus mengeset terlebih dahulu nih di configuration filenya

return array(
    .....
    'timeZone'=>'Asia/Jakarta',
    .....
)

Buat Sebuah Class DateHelper di Components ini hasil nyari di stackoverflow..


class DateHelper{
 public static function get_time_ago($time_stamp)
 {
 // $time_difference = Yii::app()->localtime->localNow - $time_stamp;
  $time_difference = strtotime('now') - $time_stamp;

  if ($time_difference >= 60 * 60 * 24 * 365.242199)
  {
   /*
    * 60 seconds/minute * 60 minutes/hour * 24 hours/day * 365.242199 days/year
   * This means that the time difference is 1 year or more
   */
   return DateHelper::get_time_ago_string($time_stamp, 60 * 60 * 24 * 365.242199, 'year');
  }
  elseif ($time_difference >= 60 * 60 * 24 * 30.4368499)
  {
   /*
    * 60 seconds/minute * 60 minutes/hour * 24 hours/day * 30.4368499 days/month
   * This means that the time difference is 1 month or more
   */
   return DateHelper::get_time_ago_string($time_stamp, 60 * 60 * 24 * 30.4368499, 'month');
  }
  elseif ($time_difference >= 60 * 60 * 24 * 7)
  {
   /*
    * 60 seconds/minute * 60 minutes/hour * 24 hours/day * 7 days/week
   * This means that the time difference is 1 week or more
   */
   return DateHelper::get_time_ago_string($time_stamp, 60 * 60 * 24 * 7, 'week');
  }
  elseif ($time_difference >= 60 * 60 * 24)
  {
   /*
    * 60 seconds/minute * 60 minutes/hour * 24 hours/day
   * This means that the time difference is 1 day or more
   */
   return DateHelper::get_time_ago_string($time_stamp, 60 * 60 * 24, 'day');
  }
  elseif ($time_difference >= 60 * 60)
  {
   /*
    * 60 seconds/minute * 60 minutes/hour
   * This means that the time difference is 1 hour or more
   */
   return DateHelper::get_time_ago_string($time_stamp, 60 * 60, 'hour');
  }
  else
  {
   /*
    * 60 seconds/minute
   * This means that the time difference is a matter of minutes
   */
   return DateHelper::get_time_ago_string($time_stamp, 60, 'minute');
  }
 }

 public static function get_time_ago_string($time_stamp, $divisor, $time_unit)
 {
  $time_difference = strtotime("now") - $time_stamp;
  $time_units      = floor($time_difference / $divisor);

  settype($time_units, 'string');

  if ($time_units === '0')
  {
   return 'less than 1 ' . $time_unit . ' ago';
  }
  elseif ($time_units === '1')
  {
   return '1 ' . $time_unit . ' ago';
  }
  else
  {
   /*
    * More than "1" $time_unit. This is the "plural" message.
   */
   // TODO: This pluralizes the time unit, which is done by adding "s" at the end; this will not work for i18n!
   return $time_units . ' ' . $time_unit . 's ago';
  }
 }
}

Okeh untuk mengetesnya kita bisa pakai seperti ini..

 echo DateHelper::get_time_ago(strtotime($data->date_time));


Kalau mau pake di sisi client bisa pake extensi yii timeago disini
http://www.yiiframework.com/extension/timeago/
Semoga bermanfaat....
Facebook Comments

1 komentar: