Time Ago or Time To Go function in php

Introduction

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

Generally you want 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. You may need to display also how many seconds, minutes or even days “to go” to happen something in future. In real life scenarios, the examples may be, Pepsi IPL 15 days to go, Brasil Football World Cup 5 days to go, etc.

Most of you have already seen the time ago functionality for the comment system in Facebook, Linkedin, Twitter, etc. So for this kind of message to display, you need a function which will show you the similar format for the time.

Prerequisites

PHP 7.4.3/8.3.9

Time Ago or Time to Go

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 is in the future time
			$difference = -$difference;
			$ending = "to go";
		}
		
		//echo '$difference: ' . $difference;
		
		for ($j = 0; $j < count($lengths) && $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:

$date = date('Y-m-d H:i:s');
echo nl2br($date. ' -> ' . time_ago($date)."\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:

2025-08-25 07:17:59 -> 0 sec to go
2014-04-10 14:20:15 -> 1 decade ago
2016-07-20 14:20:15 -> 9 years ago
2019-08-13 05:43:45 -> 6 years ago

Source Code

Download

Share

Related posts

1 comment

  1. Javed Ur Rehman

    Soumitra can you please change the link in my precious comment, as the link is changed now. My tutorial is now moved to http://www.allphptricks.com/php-time-ago-function/
    Thanks

    Reply

Leave a comment