fork(1) download
  1. <?php
  2. $datetime = '2013-05-01 00:22:35';
  3. function time_elapsed_string($datetime, $full = false) {
  4. $now = new DateTime;
  5. $ago = new DateTime($datetime);
  6. $diff = $now->diff($ago);
  7.  
  8. $diff->w = floor($diff->d / 7);
  9. $diff->d -= $diff->w * 7;
  10.  
  11. $string = array(
  12. 'y' => 'year',
  13. 'm' => 'month',
  14. 'w' => 'week',
  15. 'd' => 'day',
  16. 'h' => 'hour',
  17. 'i' => 'minute',
  18. 's' => 'second',
  19. );
  20. foreach ($string as $k => &$v) {
  21. if ($diff->$k) {
  22. $v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');
  23. } else {
  24. unset($string[$k]);
  25. }
  26. }
  27.  
  28. if (!$full) $string = array_slice($string, 0, 1);
  29. return $string ? implode(', ', $string) . ' ago' : 'just now';
  30. }
  31.  
  32. echo time_elapsed_string('2017-06-01 00:22:35',true);
  33. ?>
Success #stdin #stdout 0s 83904KB
stdin
Standard input is empty
stdout
3 weeks, 5 days, 3 hours, 25 minutes, 6 seconds ago