facebook style "time ago" function

Coding Critique is the place to post source code for peer review by other members of DevNetwork. Any kind of code can be posted. Code posted does not have to be limited to PHP. All members are invited to contribute constructive criticism with the goal of improving the code. Posted code should include some background information about it and what areas you specifically would like help with.

Popular code excerpts may be moved to "Code Snippets" by the moderators.

Moderator: General Moderators

User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: facebook style "time ago" function

Post by s.dot »

Cool that is very valuable information. So I suppose when using lookup tables vs a bunch of if's (if worried about performance), it would depend on the number and complexity of the evaluations.

Of course, when profiling code, if this is the biggest speed problem then one is in great shape!
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Re: facebook style "time ago" function

Post by Luke »

I should mention I took a lot of inspiration from Django's "humanize" for my code. So you could look at that as well. :)
marcdonaldson
Forum Newbie
Posts: 1
Joined: Tue Aug 28, 2012 5:34 pm

Re: facebook style "time ago" function

Post by marcdonaldson »

Hi everyone,

Here is my example of time_passed and it seems to perform quicker and also caters for different formats passed in and for time in the future

Code: Select all

       /**
	* 
	* This function calculates how long ago a certain
	* date passed in was. 
	*
	*/
	
	define('SEC_IN_MINUTE',60); /* How many seconds are in a minute */
	define('SEC_IN_HOUR',3600); /* How many seconds are in an hour */
	define('SEC_IN_DAY',86400);	/* How many seconds are in a day */
	define('SEC_IN_WEEK',604800); /* How many seconds are in a week */
	define('SEC_IN_MONTH',2630880);	/* How many seconds are in a month */
	define('SEC_IN_YEAR',31556926); /* How many seconds are in a year */
	
	function md_time_passed($d) {
		
		/**
		 * This code block checks to see what type of date is 
		 * passed in and then obtains the difference in
		 * seconds.
		 */

		if (empty($d)) {
                   return('No date provided');
                }

		if ($d instanceof DateTime) {
			$c = new DateTime();
			$cTime = $c->getTimestamp();
			$dTime = $d->getTimestamp();		
			$diff = $cTime-$dTime;	
		} elseif (is_array($d) && isset($d["mon"])) {
			$d = $d[0];
			$c = time();
			$diff = $c-$d;	
		} elseif (is_string($d)) {
			$d = strtotime($d);
			$c = time();
			$diff = $c-$d;	
		} else {
			$c = time();
			$diff = $c-$d;	
		}
		
		$agostate = ($diff>=0) ? "ago" : "from now";
		
		$diff = abs($diff);
		
		/**
		 * This section of code converts the seconds in
		 * single units based on whether it is a year, month,
		 * week, day, hour, minute or second
		 */
	
		if ($diff>=SEC_IN_YEAR) {
			$diff = floor($diff / SEC_IN_YEAR);						
			$suffix = "year";
		} elseif ($diff>=SEC_IN_MONTH) {
			$diff = floor($diff / SEC_IN_MONTH);						
			$suffix = "month";		
		} elseif ($diff>=SEC_IN_WEEK) {
			$diff = floor($diff / SEC_IN_WEEK);						
			$suffix = "week";			
		} elseif ($diff>=SEC_IN_DAY) {
			$diff = floor($diff / SEC_IN_DAY);						
			$suffix = "day";				
		} elseif ($diff>=SEC_IN_HOUR) {
			$diff = floor($diff / SEC_IN_HOUR);						
			$suffix = "hour";			
		} elseif ($diff>=SEC_IN_MINUTE) {
			$diff = floor($diff / SEC_IN_MINUTE);						
			$suffix = "minute";			
		} elseif ($diff>0) {
			$suffix = "second";					
		} else {
			return("Just Now");
		}
		
		return(($diff==1) ? "{$diff} {$suffix} {$agostate}" : "{$diff} {$suffix}s {$agostate}");
	}
Please let me know your thoughts

Marc
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: facebook style "time ago" function

Post by Benjamin »

Looks great :)
Post Reply