PHP

Cara Menghitung Selisih Waktu (Time Ago) di PHP

Fungsi untuk mengubah format tanggal database menjadi format "2 menit yang lalu", "1 jam yang lalu", dsb. Sangat cocok untuk sistem komentar atau postingan.

function time_ago($timestamp) {
    $time_ago = strtotime($timestamp);
    $current_time = time();
    $time_difference = $current_time - $time_ago;
    $seconds = $time_difference;
    
    $minutes = round($seconds / 60);
    if($seconds <= 60) return "Baru saja";
    else if($minutes <= 60) return "$minutes menit yang lalu";
    else return date('d M Y', $time_ago);
}
5 Views
Eksplor Materi Lainnya