Timestamp from 2 years ago as x Days x Weeks x Months x Years ago

How can i take a timestamp from say, 2 years ago and have the output as:
2 years 3 months 2 weeks and 3 days.

it is in answer to the question "How long have i been Representing this person?":
"you have represented them for........: 2 years 3 months 2 weeks and 3 days.

Using PHP
function _represented_for($timestamp){
 
(what goes here??)
 
return "2 years, 3 months, 2 weeks and 3 days.";
}

Open in new window

LVL 1
ussherAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ussherAuthor Commented:
found it here:
http://blog.sampsonvideos.com/2009/06/17/twitter-style-timestamps-with-kohana/
/**
 * Returns time difference between two timestamps, in human readable format.
 *
 * @param   integer       timestamp
 * @param   integer       timestamp, defaults to the current time
 * @param   string        formatting string
 * @return  string|array
 */
function timespan($time1, $time2 = NULL, $output = 'years,months,weeks,days,hours,minutes,seconds')
{
	// Array with the output formats
	$output = preg_split('/[^a-z]+/', strtolower((string) $output));
		// Invalid output
	if (empty($output))
		return FALSE;
		// Make the output values into keys
	extract(array_flip($output), EXTR_SKIP);
		// Default values
	$time1  = max(0, (int) $time1);
	$time2  = empty($time2) ? time() : max(0, (int) $time2);
		// Calculate timespan (seconds)
	$timespan = abs($time1 - $time2);
		// All values found using Google Calculator.
	// Years and months do not match the formula exactly, due to leap years.
		// Years ago, 60 * 60 * 24 * 365
	isset($years) and $timespan -= 31556926 * ($years = (int) floor($timespan / 31556926));
		// Months ago, 60 * 60 * 24 * 30
	isset($months) and $timespan -= 2629744 * ($months = (int) floor($timespan / 2629743.83));
		// Weeks ago, 60 * 60 * 24 * 7
	isset($weeks) and $timespan -= 604800 * ($weeks = (int) floor($timespan / 604800));
		// Days ago, 60 * 60 * 24
	isset($days) and $timespan -= 86400 * ($days = (int) floor($timespan / 86400));
		// Hours ago, 60 * 60
	isset($hours) and $timespan -= 3600 * ($hours = (int) floor($timespan / 3600));
		// Minutes ago, 60
	isset($minutes) and $timespan -= 60 * ($minutes = (int) floor($timespan / 60));
		// Seconds ago, 1
	isset($seconds) and $seconds = $timespan;
		// Remove the variables that cannot be accessed
	unset($timespan, $time1, $time2);
		// Deny access to these variables
	$deny = array_flip(array('deny', 'key', 'difference', 'output'));
		// Return the difference
	$difference = array();
	foreach ($output as $key)
	{
		if (isset($$key) AND ! isset($deny[$key]))
		{
			// Add requested key to the output
			$difference[$key] = $$key;
		}
	}
		// Invalid output formats string
	if (empty($difference))
		return FALSE;
		// If only one output format was asked, don't put it in an array
	if (count($difference) === 1)
		return current($difference);
		// Return array
	return $difference;
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
XemorphCommented:
I find this example a bit more simple.  Though maybe not as robust.  Again, leap years could throw off the results a bit.
function _represented_for($timestamp){
 
	$now = time();
	if(is_numeric($timestamp)){
		
		// $time_diff = number of secs from the time given to now.
		$time_diff = $now - $timestamp;
		
		// Subtract 1970 to get the correct year.
		$years = (int)date('Y', $time_diff) - 1970;
		$months = (int)date('j', $time_diff);
		$days = (int)date('', $time_diff);
		
		return $years. ' years, '.$months.' months, '.$days.' days.';
	}
}

Open in new window

0
ussherAuthor Commented:
Hey, Thanks Xemorph.  Yep that would have done fine too.  Got it working now.  If i ever refer back to this it will be good to have your example here for reference as well.

Thanks very much
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.