Return to Snippet

Revision: 11669
at February 13, 2009 10:54 by jstnjns


Initial Code
function relative_time($date) {
	$diff = time() - strtotime($date);
	if ($diff>0) {
		if ($diff<60)
			return $diff . " second" . plural($diff) . " ago";
		$diff = round($diff/60);
		if ($diff<60)
			return $diff . " minute" . plural($diff) . " ago";
		$diff = round($diff/60);
		if ($diff<24)
			return $diff . " hour" . plural($diff) . " ago";
		$diff = round($diff/24);
		if ($diff<7)
			return $diff . " day" . plural($diff) . " ago";
		$diff = round($diff/7);
		if ($diff<4)
			return $diff . " week" . plural($diff) . " ago";
		return "on " . date("F j, Y", strtotime($date));
	} else {
		if ($diff>-60)
			return "in " . -$diff . " second" . plural($diff);
		$diff = round($diff/60);
		if ($diff>-60)
			return "in " . -$diff . " minute" . plural($diff);
		$diff = round($diff/60);
		if ($diff>-24)
			return "in " . -$diff . " hour" . plural($diff);
		$diff = round($diff/24);
		if ($diff>-7)
			return "in " . -$diff . " day" . plural($diff);
		$diff = round($diff/7);
		if ($diff>-4)
			return "in " . -$diff . " week" . plural($diff);
		return "on " . date("F j, Y", strtotime($date));
	}
}

Initial URL
http://www.jstnjns.com/

Initial Description
Original script by jaytee.  I used the script in a project, but then needed 'future' support.  Added the script, thought I would share.

Initial Title
Relative Time - Includes Future

Initial Tags


Initial Language
PHP