Get time ago functionality instead of date display in PHP

by barkkathulla 2014-07-19 13:12:47

Get time ago functionality instead of date display in PHP
First of all, set default timestamp as below
<?php
date_default_timezone_set('Asia/Calcutta');
$pass_timestamp="2014-07-17 18:26:22";

function get_time_interval($get_timestamp, $full = false) {
    $current_date_time = new DateTime;
    $ago = new DateTime($get_timestamp);
    $variation = $current_date_time->diff($ago);

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

    $period = array(
        'y' => 'year',
        'm' => 'month',
        'w' => 'week',
        'd' => 'day',
        'h' => 'hour',
        'i' => 'minute',
        's' => 'second',
    );
    foreach ($period as $per => &$get_period) {
        if ($variation->$per) {
            $get_period = $variation->$per . ' ' . $get_period . ($variation->$per > 1 ? 's' : '');
        } else {
            unset($period[$per]);
        }
    }

    if (!$full) $period = array_slice($period, 0, 1);
    $get_time_interval=$period ? implode(', ', $period) . ' ago' : 'just now';
	return $get_time_interval;
}

$get_output=get_time_interval($pass_timestamp, true);
echo $get_output;
?>
output:
//As per current timestamp
1 day, 18 hours, 32 minutes, 57 seconds ago
1633
like
0
dislike
0
mail
flag

You must LOGIN to add comments