fork download
  1. <?php
  2. function RelativeTime($timeToSend){
  3. $difference = time() - $timeToSend;
  4. $periods = array("sec", "min", "hour", "day", "week",
  5. "month", "years", "decade");
  6. $lengths = array("60","60","24","7","4.35","12","10");
  7.  
  8. if ($difference > 0) { // this was in the past
  9. $ending = "ago";
  10. } else { // this was in the future
  11. $difference = -$difference;
  12. $ending = "to go";
  13. }
  14. for($j = 0; array_key_exists($j,$lengths)&&$difference >= $lengths[$j]; $j++)
  15. $difference /= $lengths[$j];
  16. $difference = round($difference);
  17. if($difference != 1) $periods[$j].= "s";
  18. $text = "$difference $periods[$j] $ending";
  19. return $text;
  20. }
  21. echo RelativeTime(strtotime('-3 months'))."\n";
  22. echo RelativeTime(strtotime(0));
  23. ?>
Success #stdin #stdout 0.02s 13112KB
stdin
Standard input is empty
stdout
3 months ago
4 decades ago