1. PHP

Menghitung waktu yang telah berlalu menggunakan PHP

Berikut ini merupakan fungsi untuk menghitung waktu yang telah berlalu (elapsed time) dengan menggunakan PHP. Contoh 2 days ago, 30 minutes ago, dsb.

function timeElapsed($datetime, $full = false) 
{
    $now = new DateTime;
    $ago = new DateTime($datetime);
    $diff = $now->diff($ago);

    $diff->w = floor($diff->d / 7);
    $diff->d -= $diff->w * 7;

    $string = array(
        'y' => 'year',
        'm' => 'month',
        'w' => 'week',
        'd' => 'day',
        'h' => 'hour',
        'i' => 'minute',
        's' => 'second',
    );
    foreach ($string as $k => &$v) {
        if ($diff->$k) {
            $v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');
        } 
        else {
            unset($string[$k]);
        }
    }
    $string = array_slice($string, 0, ($full) ? 2 : 1);
    return $string ? implode(', ', $string) . ' ago' : 'just now';
}
Comments to: Menghitung waktu yang telah berlalu menggunakan PHP

    Your email address will not be published. Required fields are marked *

    Attach images - Only PNG, JPG, JPEG and GIF are supported.