A better date/time?

I have this code:


$template = preg_replace('#{%datetime%}#',
  date_format(
    date_create(
      $row->TextTimestamp,
      timezone_open('America/Indianapolis')
    ), 
  'l F jS Y g:i:s A'),
$template);

I would like the date and time to actually work like Twitter or Facebook. (posted 3 minutes ago, or 2 days 40 minutes ago, 6 months 28 days ago, etc).

What do I need to do to accomplish this?

Here is a function I found that does this perfectly:


	/****************************************
	 * Human Readable Date and Time
	 * ------------------------------------ 
	 * @params	<str> $timestamp			
	 * @returns <str> $text					
	 *										
	 * Returns the timestamp like Facebook	
	 *										
	 ****************************************/
	function RelativeTime($timestamp)
	{
                // In case the timestamp is not a unix timestamp, we need to make it one. This won't work with a MySQL timestamp.
		$timestamp = strtotime($timestamp);

                // Original function as follows:
		$difference = time() - $timestamp;
		$periods = array("sec", "min", "hour", "day", "week",
		"month", "years", "decade");
		$lengths = array("60","60","24","7","4.35","12","10");

		if ($difference > 0) { // this was in the past
		$ending = "ago";
		} else { // this was in the future
		$difference = -$difference;
		$ending = "to go";
		}
		for($j = 0; $difference >= $lengths[$j]; $j++)
		$difference /= $lengths[$j];
		$difference = round($difference);
		if($difference != 1) $periods[$j].= "s";
		$text = "$difference $periods[$j] $ending";
		return $text;
	}
	/* End function:
	   RelativeTime() */


Found it here: http://www.weberdev.com/get_example-4769.html

that sounds totally overwhelming. guess i’ll stick with the ugly timestamp instead.

You will need a database with a small table for each message posted. For each message you would place the current date and time, then when you view the message after you will have php calculate the time difference from the date of post.