Link to home
Start Free TrialLog in
Avatar of ussher
ussherFlag for Japan

asked on

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

ASKER CERTIFIED SOLUTION
Avatar of ussher
ussher
Flag of Japan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

Avatar of ussher

ASKER

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