fork download
  1. <?php
  2. function time_since($since)
  3. {
  4. $chunks = array(
  5. array(60 * 60 * 24 * 365 , 'year'),
  6. array(60 * 60 * 24 * 30 , 'month'),
  7. array(60 * 60 * 24 * 7, 'week'),
  8. array(60 * 60 * 24 , 'day'),
  9. array(60 * 60 , 'hour'),
  10. array(60 , 'minute'),
  11. array(1 , 'second')
  12. );
  13.  
  14. for ($i = 0, $j = count($chunks); $i < $j; $i++) {
  15. $seconds = $chunks[$i][0];
  16. $name = $chunks[$i][1];
  17. if (($count = floor($since / $seconds)) != 0) {
  18. break;
  19. }
  20. }
  21.  
  22. $print = ($count == 1) ? '1 '.$name : "$count {$name}s";
  23. return $print;
  24. }
  25. $DT = strtotime("2013-04-10");
  26. echo time_since(time()-$DT);
  27.  
  28. ?>
Success #stdin #stdout 0.01s 20520KB
stdin
Standard input is empty
stdout
1 month