Posted By

natalie on 11/09/06


Tagged

php time data sort calendar date Shell python function linux perl timesince relative glob series60 pim


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

gartenstuhl


Timesince


 / Published in: Other
 

  1.  
  2. <?php
  3.  
  4. /* Works out the time since the entry post, takes a an argument in unix time (seconds) */
  5. function time_since($original) {
  6. // array of time period chunks
  7. $chunks = array(
  8. array(60 * 60 * 24 * 365 , 'year'),
  9. array(60 * 60 * 24 * 30 , 'month'),
  10. array(60 * 60 * 24 * 7, 'week'),
  11. array(60 * 60 * 24 , 'day'),
  12. array(60 * 60 , 'hour'),
  13. array(60 , 'minute'),
  14. );
  15.  
  16. $today = time(); /* Current unix time */
  17. $since = $today - $original;
  18.  
  19. // $j saves performing the count function each time around the loop
  20. for ($i = 0, $j = count($chunks); $i < $j; $i++) {
  21.  
  22. $seconds = $chunks[$i][0];
  23. $name = $chunks[$i][1];
  24.  
  25. // finding the biggest chunk (if the chunk fits, break)
  26. if (($count = floor($since / $seconds)) != 0) {
  27. // DEBUG print "<!-- It's $name -->\n";
  28. break;
  29. }
  30. }
  31.  
  32. $print = ($count == 1) ? '1 '.$name : "$count {$name}s";
  33.  
  34. if ($i + 1 < $j) {
  35. // now getting the second item
  36. $seconds2 = $chunks[$i + 1][0];
  37. $name2 = $chunks[$i + 1][1];
  38.  
  39. // add second item if it's greater than 0
  40. if (($count2 = floor(($since - ($seconds * $count)) / $seconds2)) != 0) {
  41. $print .= ($count2 == 1) ? ', 1 '.$name2 : ", $count2 {$name2}s";
  42. }
  43. }
  44. return $print;
  45. }
  46.  
  47. ?>
  48.  

Report this snippet  

You need to login to post a comment.