Solved

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

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

0
Comment
Question by:ussher
  • 2
3 Comments
 
LVL 1

Accepted Solution

by:
ussher earned 0 total points
ID: 24820265
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
 
LVL 4

Expert Comment

by:Xemorph
ID: 24820342
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
 
LVL 1

Author Comment

by:ussher
ID: 24820710
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

791 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