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

Apache HTTP Server 2.4, PHP 7.4.3

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 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:

time ago or time to go function php
Current Date and Time -> 2020-11-01 08:59:54
2014-04-10 14:20:15 -> 7 years ago
2016-07-20 14:20:15 -> 4 years ago
2019-08-13 05:43:45 -> 1 year ago

Source Code

Download

3 Thoughts to “Time Ago or Time To Go function in php”

  1. This is really helpful tutorial, thanks to share with us :) I also recently wrote another PHP time ago function, hope this also help.
    http://www.allphptricks.com/php-time-ago-function/

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

Leave a Comment