[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2009-07-09
3
Medium Priority
?
582 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
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.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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 …
Suggested Courses

656 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