[trick] Membuat Selisih Waktu Komentar Ala Facebook dengan PHP OOP

aaa

class.php


<?php
class selisih {

function selisih_tanggal($str) {

list($tgl, $wkt) = explode(' ', $str);
list($tahun, $bulan, $hari) = explode('-', $tgl);
list($jam, $menit, $detik) = explode(':', $wkt);
$wkt_skrng = mktime($jam, $menit, $detik, $bulan, $hari, $tahun);
return $wkt_skrng;
}

function selisih_jam($wkt_skrng){

$beda = time() - $wkt_skrng;
$periode = array("sec", "min", "hr", "day", "week", "month", "year", "decade");
$ukuran = array("60","60","24","7","4.35","12","10");
for($i = 0; $beda >= $ukuran[$i]; $i++)
$beda /= $ukuran[$i];
$beda = round($beda);
if($beda != 1) $periode[$j].= "s";
$text = "$beda $periode[$j] ";
return $text;
}

}
?>

code.php


<?php
include_once("class.php");
$Object = new selisih;
$ts = "2010-01-30 20:19:18";
$cari = ($Object -> selisih_tanggal($ts));
$kapannya = ($Object -> selisih_jam($cari));
?>

<h2><?php echo $kapannya; ?></h2>

 

Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: