Part of Slepp's ProjectsPastebinTURLImagebinFilebin
Feedback -- English French German Japanese
Create Upload Newest Tools Donate
Sign In | Create Account

Stuff
Friday, June 19th, 2009 at 7:59:43am MDT 

  1. function timeDiff($time, $opt = array()) {
  2. // The default values
  3. $defOptions = array(
  4. ‘to’ => 0,
  5. ‘parts’ => 1,
  6. ‘precision’ => ’second’,
  7. ‘distance’ => TRUE,
  8. ’separator’ => ‘, ‘
  9. );
  10. $opt = array_merge($defOptions, $opt);
  11. // Default to current time if no to point is given
  12. (!$opt['to']) && ($opt['to'] = time());
  13. // Init an empty string
  14. $str = ”;
  15. // To or From computation
  16. $diff = ($opt['to'] > $time) ? $opt['to']-$time : $time-$opt['to'];
  17. // An array of label => periods of seconds;
  18. $periods = array(
  19. ‘decade’ => 315569260,
  20. ‘year’ => 31556926,
  21. ‘month’ => 2629744,
  22. ‘week’ => 604800,
  23. ‘day’ => 86400,
  24. ‘hour’ => 3600,
  25. ‘minute’ => 60,
  26. ’second’ => 1
  27. );
  28. // Round to precision
  29. if ($opt['precision'] != ’second’)
  30. $diff = round(($diff/$periods[$opt['precision']])) * $periods[$opt['precision']];
  31. // Report the value is ‘less than 1 ‘ precision period away
  32. (0 == $diff) && ($str = ‘less than 1 ‘.$opt['precision']);
  33. // Loop over each period
  34. foreach ($periods as $label => $value) {
  35. // Stitch together the time difference string
  36. (($x=floor($diff/$value))&&$opt['parts']) && $str.=($str?$opt['separator']:”).($x.’ ‘.$label.($x>1?’s’:”));
  37. // Stop processing if no more parts are going to be reported.
  38. if ($opt['parts'] == 0 || $label == $opt['precision']) break;
  39. // Get ready for the next pass
  40. $diff -= $x*$value;
  41. }
  42. $opt['distance'] && $str.=($str&&$opt['to']>$time)?’ ago’:”;
  43. return $str;
  44. }
  45.  
  46. http://www.isthisablog.com/2009/06/19/php-elapsed-time-function/comment-page-1/#comment-2962#ixzz0IstcSgQ1&D

advertising

Update the Post

Either update this post and resubmit it with changes, or make a new post.

You may also comment on this post.

update paste below
details of the post (optional)

Note: Only the paste content is required, though the following information can be useful to others.

Save name / title?

(space separated, optional)



Please note that information posted here will expire by default in one month. If you do not want it to expire, please set the expiry time above. If it is set to expire, web search engines will not be allowed to index it prior to it expiring. Items that are not marked to expire will be indexable by search engines. Be careful with your passwords. All illegal activities will be reported and any information will be handed over to the authorities, so be good.

worth-right
fantasy-obligation