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

Posted on 2009-07-09
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

Question by:ussher
  • 2

Accepted Solution

ussher earned 0 total points
Comment Utility
found it here:

 * 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


Expert Comment

Comment Utility
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


Author Comment

Comment Utility
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

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now