Ago function in PHP

We always required time ago in our website. Here we have function for the same. You can enjoy with the embed in your website. Example of Ago function in PHP as below

Ago Function

$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.

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.