PHP Time Ago Function


PHP Time Ago Function

Demo Download

Time Ago Function is a function that is commonly used to display time in human understandable format, as you have seen many times on social networks, whenever you post any status on your Facebook, Twitter or any other network, it says posted few seconds ago, 1 min ago, 1 hour age, etc. This is actually done by subtracting the current time from the post published time.

For this purpose we need to store a date in database, date should be in MySQL standard date and time format (“Y-m-d H:i:s”).

Lets create a database with name register.

CREATE DATABASE register;

Create a table with name time_table with column id and trn_date.

CREATE TABLE IF NOT EXISTS `time_table` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `trn_date` datetime NOT NULL,
 PRIMARY KEY (`id`)
 );

After creating the above table, you need to insert a record, or I have attached SQL file if you can import that SQL file so it is better because it will already have data in table.

Now create php file with the following code:

<?php
//Creating Function
function TimeAgo ($oldTime, $newTime) {
$timeCalc = strtotime($newTime) - strtotime($oldTime);
if ($timeCalc >= (60*60*24*30*12*2)){
	$timeCalc = intval($timeCalc/60/60/24/30/12) . " years ago";
	}else if ($timeCalc >= (60*60*24*30*12)){
		$timeCalc = intval($timeCalc/60/60/24/30/12) . " year ago";
	}else if ($timeCalc >= (60*60*24*30*2)){
		$timeCalc = intval($timeCalc/60/60/24/30) . " months ago";
	}else if ($timeCalc >= (60*60*24*30)){
		$timeCalc = intval($timeCalc/60/60/24/30) . " month ago";
	}else if ($timeCalc >= (60*60*24*2)){
		$timeCalc = intval($timeCalc/60/60/24) . " days ago";
	}else if ($timeCalc >= (60*60*24)){
		$timeCalc = " Yesterday";
	}else if ($timeCalc >= (60*60*2)){
		$timeCalc = intval($timeCalc/60/60) . " hours ago";
	}else if ($timeCalc >= (60*60)){
		$timeCalc = intval($timeCalc/60/60) . " hour ago";
	}else if ($timeCalc >= 60*2){
		$timeCalc = intval($timeCalc/60) . " minutes ago";
	}else if ($timeCalc >= 60){
		$timeCalc = intval($timeCalc/60) . " minute ago";
	}else if ($timeCalc > 0){
		$timeCalc .= " seconds ago";
	}
return $timeCalc;
}

//Connecting Database
$connection = mysql_connect('localhost', 'root', '');
if (!$connection){
	die("Database Connection Failed" . mysql_error());
	}
$select_db = mysql_select_db('register');
if (!$select_db){
	die("Database Selection Failed" . mysql_error());
	}
$query_time="Select * from time_table where id=1";
$result_time = mysql_query($query_time);
$row_time = mysql_fetch_array($result_time);
$date = $row_time['trn_date'];

echo TimeAgo($date, date("Y-m-d H:i:s"));
?>

Note: In the above code first I created a function to calculate posted time and then I used mysql_connect() function, you will need to provide your database username and password, then I simply echo time ago function with parameters.

Update: Try to use mysqli instead of mysql, this is old tutorial therefore I used mysql in it.

Demo Download

If you have any issue with this tutorial so let me know in the below comments section.

Facebook Official Page: All PHP Tricks

Twitter Official Page: All PHP Tricks

Article By
Javed Ur Rehman is a passionate blogger and web developer, he loves to share web development tutorials and blogging tips. He usually writes about HTML, CSS, JavaScript, Jquery, Ajax, PHP and MySQL.
  1. This helped me perfectly. Thanks Javed.

    I am just wondering what we would add to the code if the date stored in mysql is in the future. Currently it just shows number of seconds in negative. How would we go about it in making the seconds display a human-readable time?

  2. Thanks for your tutorial Sir. The issue I’m facing is that the minimum time ago it can set is an hour and beyond. Would really appreciate your help.

  3. Hello there, everything is somewhat fine but there is an error if the date in the parameter is nearer to today. I mean it gives me negative 5 digits (-19678).

    Please what do you is the problem?

      1. Is there a way to make the negative seconds show in human-readable time IF the date parameter in MySQL is in the FUTURE, not the past?

  4. Hey can you please do a time ago function in mysqli please. I love your tutorial and I would love your work more.

  5. Javed Ur Rehman, all ur tutorials have been 100% working and helpful, easily modified better understood. i like you man. keep up the good work.

Leave a Reply

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