PHP 

Introduction

Time ago or time to go function in PHP shows how to display how much time already passed or how much time to go for a given date time.

We have to implement this kind of functionality in many scenarios like someone has posted comment on your blog, tutorial, product or article then you want to show how much time ago that particular comment was posted. The time ago function may display the time format like – 1 sec ago, 2 secs ago, 1 min ago, 3 mins ago or 4 days ago etc.

Now we may need to display also how many seconds, minutes or even days “to go” to happen something. In real life scenarios like Pepsi IPL 15 days to go, Brasil Football World Cup 5 days to go etc.

Most of us have already seen the time ago functionality for the comment system in Facebook, Linkedin, Twitter etc..

So for this kind of message to display, we need a function which will show us the similar format for the time.

Example and Source Code

The below function takes one argument of type date time. First the function validates whether the given date time is in valid format or not. If the date time format is valid then it calculates how much time has already been passed or how much time is left for a given date time.

Date Validation

Below function checks whether the date time is in valid format or not:

function is_date_time_valid($date) {
	
	if (date('Y-m-d H:i:s', strtotime($date)) == $date) {
		return TRUE;
	} else {
		return FALSE;
	}
}

Time Ago or Time To Go

The below function implements time ago or time to go functionality.

function time_ago($date) {
	$is_valid = is_date_time_valid($date);
	
	if ($is_valid) {
		$timestamp = strtotime($date);
		$difference = time() - $timestamp;
		$periods = array("sec", "min", "hour", "day", "week", "month", "year", "decade");
		$lengths = array("60", "60", "24", "7", "4.35", "12", "10");

		if ($difference > 0) { // this was in the past time
			$ending = "ago";
		} else { // this was in the future time
			$difference = -$difference;
			$ending = "to go";
		}
		
		for ($j = 0; $difference >= $lengths[$j]; $j++)
			$difference /= $lengths[$j];
		
		$difference = round($difference);
		
		if ($difference > 1)
			$periods[$j].= "s";
		
		$text = "$difference $periods[$j] $ending";
		
		return $text;
	} else {
		return 'Date Time must be in "yyyy-mm-dd hh:mm:ss" format';
	}
}

Usage Examples

Create below examples as of now:

echo nl2br('Current Date and Time -> '.date('Y-m-d H:i:s')."\n");

$date = '2014-04-10 14:20:15';
echo nl2br($date. ' -> ' . time_ago($date)."\n");

$date = '2016-07-20 14:20:15';
echo nl2br($date. ' -> ' . time_ago($date)."\n");

$date = '2019-08-13 05:43:45';
echo nl2br($date. ' -> ' . time_ago($date)."\n");

Output

You will see below output for the above example dates:

Current Date and Time -> 2019-08-13 12:18:38
2014-04-10 14:20:15 -> 5 years ago
2016-07-20 14:20:15 -> 3 years ago
2019-08-13 05:43:45 -> 7 hours ago

Source Code

You can download source code.

That’s all. Thanks for reading.

3 thoughts on “Time Ago or Time To Go function in php

Leave a Reply

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