FREE PHP Scripts and Snippets

Ready to use Free PHP scripts and snippets to use in your projects.
All Free Scripts & Snippets Complete PHP Scripts

[PHP] Time ago calculation function

>> Share on Facebook & Impress your friends <<

It is a very nice PHP time ago function. You gave pass any date to it and it will tell if it was few seconds ago, few months ago or it happened years back.

<?php

/*
PHP  SCRIPT (function / snippet)

NAME: Php time ago calculation function

It is a very nice PHP time ago function.

You can give this function any date of the past and it will return output like :
2 minutes ago
5 hours ago
8 montha ago
3 years ago


*/


function timeAgoClaculator($date){ 

// Store provided date properly
    $timeToUse = strtotime($date); 
	
	// This is the current time
    $currentTime = time(); 
	
	// Here we calculate the difference i.e. the "the time ago"
    $timeAgo = $currentTime - $timeToUse; 
	
	// If time ago is less than 60 seconds
    if($timeAgo < 60){ 
        $roundedTime = round($timeAgo); 
        $seconds = ($roundedTime == 1)?"second":"seconds"; 
        return "$roundedTime $seconds ago"; 
		
		// I it is in minutes, i.e. time ago is less than an hour
    }elseif($timeAgo < 3600){ 
        $roundedTime = round($timeAgo / 60); 
        $minutes = ($roundedTime == 1)?"minute":"minutes"; 
        return "$roundedTime $minutes ago"; 
		
		// If time ago is in hours i.e. less than a day
    }elseif($timeAgo >= 3600 && $timeAgo < 86400){ 
        $roundedTime = round($timeAgo / 60 / 60); 
        $h = ($roundedTime == 1)?"hour":"hours"; 
        return "$roundedTime $h ago"; 
		
		// If time ago is in days i.e. less than a month
    }elseif($timeAgo >= 86400 && $timeAgo < 2629743.83){ 
        $roundedTime = round($timeAgo / 60 / 60 / 24); 
        $d = ($roundedTime == 1)?"day":"days"; 
        return "$roundedTime $d ago"; 
		
		// If time ago is in months
    }elseif($timeAgo >= 2629743.83 && $timeAgo < 31556926){ 
        $roundedTime = round($timeAgo / 60 / 60 / 24 / 30.4375); 
        $months = ($roundedTime == 1)?"month":"months"; 
        return "$roundedTime $months ago"; 
		
		// Finally if the event happened years ago
    }else{ 
        $roundedTime = round($timeAgo / 60 / 60 / 24 / 365); 
        $years = ($roundedTime == 1)?"year":"years"; 
        return "$roundedTime $years ago"; 
    } 
} 


// Now let us test the PHP time ago function
echo timeAgoClaculator("2016-06-05 00:01:15")."<br />"; 
echo timeAgoClaculator("2016-06-04 22:12:16")."<br />"; 
echo timeAgoClaculator("2015-05-05 22:22:25")."<br />"; 
echo timeAgoClaculator("2012-02-22 11:12:12")."<br />"; 


?>


Request Custom Snippet

Just $5 Onwards

 

Previously Ordered Snippets

I want counter to a date. E.g. 5 years, 3 months, 5 days, 2 hours and 1 seconds left.
Price $5 Order Now

I want reverse of X time ago. I want so and so even coming up in X days or Y months and so on. Basically a PHP time to go function.
Price $5 Order Now

I Want a simple function to display a calendar and highlight a specific date on it.
Price $10 Order Now

I want a date count down counter which is dynamic and updates via javascript.
Price $5 Order Now

Random PHP Scripts and Snippets

PHP extract multiple emails from text

PHP Script to scan a text and extract all email ids contained in the text. It outputs email addresses as...

Free PHP folder to gallery script

100% Free PHP folder to gallery script. Just give the name of folder and it will generate gallery of all...

Extracting all numbers from a String [PHP]

This PHP function extracts all numbers from a String and prints them as a comma separated list of numbers. Ready...

PHP - Extract all links from a web page

PHP Script to extract all links from a web page. This PHP Snippet reads the contents of a web page...

Generate Random Password in PHP

PHP Script to Generate Random Password. You can set the list of characters that you want to be used in...

Detect Valid Email address with PHP

A very simple php snippet to check if an email id is valid or not. If the email address is...

[PHP] generate keywords from text function

This is a simple function written in PHP to generate keywords from any text. Just pass any free text to...

PHP Create thumbnail of an image

PHP Script to create thumbnail of any image (.jpg, .png and .gif). This create thumbnail function is ready to use....