CoderZone.org

Category: >> PHP Code >> Get Relative Dates and Times Bookmark and Share

<< lastnext >>

Snippet Name: Get Relative Dates and Times

Description: This function takes seconds as an argument and returns the time difference similar to the Facebook/Twitter does, i.e. "2 minutes ago", "9 hours ago", etc

Also see:
» Get the relative path to the root ...
» Convert Minutes to Days, Hours, an...
» LDAP Timestamp Converter
» Time left In Weeks, Days, Hours, A...
» Convert Seconds to Hours:Minutes:S...
» Calculate the difference between t...
» Date Functions: Calculate elapsed ...
» Date/Time Calculations: Get second...
» Convert Minutes to Hours
» Convert Hours to Minutes
» Convert 12 to 24 hour time and vic...
» Days in month #2
» Days in month #1
» Convert minutes to hours #2
» Convert minutes to hours #1

Comment: (none)

Author: CoderZone
Language: PHP
Highlight Mode: PHP
Last Modified: March 16th, 2009

function relativedate($secs) {
 
		$second = 1;
		$minute = 60;
		$hour = 60*60;
		$day = 60*60*24;
		$week = 60*60*24*7;
		$month = 60*60*24*7*30;
		$year = 60*60*24*7*30*365;
 
		if ($secs <= 0) { $output = "now";
		}elseif ($secs > $second && $secs < $minute) { $output = round($secs/$second)." second";
		}elseif ($secs >= $minute && $secs < $hour) { $output = round($secs/$minute)." minute";
		}elseif ($secs >= $hour && $secs < $day) { $output = round($secs/$hour)." hour";
		}elseif ($secs >= $day && $secs < $week) { $output = round($secs/$day)." day";
		}elseif ($secs >= $week && $secs < $month) { $output = round($secs/$week)." week";
		}elseif ($secs >= $month && $secs < $year) { $output = round($secs/$month)." month";
		}elseif ($secs >= $year && $secs < $year*10) { $output = round($secs/$year)." year";
		}else{ $output = " more than a decade ago"; }
 
		if ($output <> "now"){
			$output = (substr($output,0,2)<>"1 ") ? $output."s" : $output;
		}
		return $output;
	}
 
 
 
echo relativedate(75); // 1 minute, 15 seconds
 
There haven't been any comments added for this snippet yet. You may add one if you like.  Add a comment 
© coderzone.org | users online: 11