We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now


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

ussher asked
Medium Priority
Last Modified: 2013-12-13
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

Watch Question

Unlock this solution and get a sample of our free trial.
(No credit card required)

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();
		// $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


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
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.