Quick Search:
 
 PHP Code: Get Relative Dates and Times Jump to:  
Category: >> PHP Code >> Get Relative Dates and Times  

<< 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 Relative Dates and Times
» Convert Seconds to Hours:Minutes:Secon...
» Calculate the difference between two t...
» Date Functions: Calculate elapsed time...
» Date and Time Calculations: Get second...
» Convert Minutes to Hours
» Convert Hours to Minutes
» Convert Standard time to 24-hour time ...
» Days in month #2
» Days in month one
» Convert minutes to hours #2
» Convert minutes to hours one

Comment: (none)

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


 
   Home |    Search |    Code Library |    Sponsors |    Privacy |    Terms of Use |    Contact Us © 2003 - 2024 psoug.org