Ago function in PHP

Share It!

We always required time ago in our website. Here we have function for the same.. you can enjoy with the embed in your website..

How to use? Example as belows

$datetime = '2015-07-31 22:00:00';
$time = strtotime($datetime);
echo time_ago($time);
function time_ago($time='') {

	$ago_suffix 	= "ago";
	$time_frame 	= array("second", "minute", "hour", "day", "week", "month", "year", "decade");
	$time_length 	= array("60","60","24","7","4.35","12","10");

	$now = time();
	
	$time_diff     = $now - $time;

	for($j = 0; $time_diff >= $time_length[$j] && $j < count($time_length)-1; $j++) {
	   $time_diff /= $time_length[$j];
	}

	$time_diff = round($time_diff);

	if($time_diff != 1) {
	   $time_frame[$j].= "s";
	}

	return $time_diff." ".$time_frame[$j]." ".$ago_suffix;

}

Leave a Reply

Your email address will not be published.