Home PHP JavaScript CSS WordPress APIs .htaccess Other How-To Useful Scripts What I Recommend
Posted on by Aleksandar Gichevski ()
Working with time in PHP is very easy due to the predefined PHP functions that help us use and convert the real time as we need it.

PHP Time/Date Main Functions

Most used and main functions for time in php are time() which returns current Unix timestamp and date() that formats the local time in specific format by given timestamp parameter. Also very useful function is strtotime() which parses about any English textual datetime description into a Unix timestamp. All other available time/date functions that make our coding easier and simpler can be found here: http://php.net/manual/en/ref.datetime.php So I have always asked myself why on that list are missing 2 also very important functions that can calculate time remaining and time elapsed by given timestamp and current server time. I use these 2 functions often when coding my time/date related scripts and that's why I needed to create my own functions that will calculate these 2 values. Here is the code:

Time Remaining

This time remaining php function accepts timestamp as input parameter and returns array with days, hours, minutes and seconds as result.
 0) { 
	
		/*divide timeLeft value with number of seconds for 1 day:  1*24*60*60, 
		remove calculated seconds from main timeLeft value*/
        $days = floor($timeLeft / 86400); 
        $timeLeft = $timeLeft - $days * 86400; 
		
		/*divide timeLeft value with number of seconds for 1 hr:  1*60*60, 
		remove calculated seconds from main timeLeft value*/
        $hrs = floor($timeLeft / 3600); 
        $timeLeft = $timeLeft - $hrs * 3600;
		 
		/*divide timeLeft value with number of seconds for 1 min:  1*60, 
		remove calculated seconds from main timeLeft value */
        $mins = floor($timeLeft / 60); 
		
		//what is left is seconds value
        $secs = $timeLeft - $mins * 60; 
		
    } 
	else 
	{ 
		//return array with 0 values when there is not defined endTime
        return array(0, 0, 0, 0); 
    } 
	
	//return array with calculated values
    return array($days, $hrs, $mins, $secs); 
}

Time Elapsed

This time elapsed php function accepts timestamp as input parameter and returns string with elapsed time from server time.
  'year',
		30*24*60*60     =>  'month',
		24*60*60        =>  'day',
		60*60           =>  'hr',
		60              =>  'min',
		1               =>  'second'
	);

	//loop over the array
    foreach ($values as $secs => $point)
    {
		//check if timestamp is equal or bigger the array value
        $divRes = $timestamp / $secs;
        if ($divRes >= 1)
        {
			//if timestamp is bigger, round the divided value and return it
            $res = round($divRes);
            return $res . ' ' . $point . ($res > 1 ? 's' : '');
        }
    }
}

Other Posts You Might Like

Get a fast, free website audit


Enter your URL below to get full in-depth SEO report and tips.

‚Äč

Useful Scripts

Most Popular Posts

Recent Posts

June 14th, 2014
Website Optimization Using Gzip Compression

June 10th, 2014
Google Search AutoComplete API

May 14th, 2014
What is a Tag Cloud and How to Calculate it by Formula

March 27th, 2014
Error writing file /tmp (errcode 28) Solved!

February 15th, 2014
Fixed CSS & HTML Navigation Bar

February 9th, 2014
Benchmark Your Server (CPU, File IO, MySQL) with SysBench

February 8th, 2014
JavaScript Array Basics

January 29th, 2014
JVZoo IPN API in PHP

January 25th, 2014
Payoneer Debit Card for Freelancers to receive money online

January 24th, 2014
How HTML Color Codes are generated?

Read Latest Posts directly on Facebook

Archive