Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. '2013-05-01 00:22:35' -> '3 months ago'
  2.  
  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. // here is your foreach loop
  33.  
  34. // check if less than 1 hour, if so, return that
  35. $keys = array_keys($string);
  36. if ($keys[0] === 'i' || $keys[0] === 's') {
  37. return 'less than 1 hour ago';
  38. }
  39.  
  40. time_elapsed_string("2017-06-10 12:11:19"); // less than 1 hour ago
  41. time_elapsed_string("2017-06-10 11:44:00"); // 1 hour ago
  42. time_elapsed_string("2017-06-10 11:44:00", true); // 1 hour, 1 minute ago
  43. time_elapsed_string("2017-06-09 11:45:00", true); // 1 day, 1 hour ago
  44. time_elapsed_string("2017-06-09 11:45:00"); // 1 day ago
  45.  
  46. if($diff->days) {
  47. $diff->w = floor($diff->d / 7);
  48. $diff->d -= $diff->w * 7;
  49. } else /* less than a day's difference */ if(!$diff->h && ($diff->i || $diff->s)) /* less than an hour's difference */ {
  50. $diff->h = 1;
  51. $diff->i = 0;
  52. $diff->s = 0;
  53. }
  54.  
  55. function time_elapsed_string($datetime){
  56. $now=new DateTime; // current datetime
  57. $ago=new DateTime($datetime); // user datetime
  58. if($now<$ago){return "Unexpected future datetime value";}
  59. $diff=$now->diff($ago); // datetime difference
  60. $diff->w=intval(floor($diff->d/7)); // add weeks to diff output
  61. $diff->d-=$diff->w*7; // reduce days based on weeks calculation
  62. $units=[
  63. 'y'=>'year',
  64. 'm'=>'month',
  65. 'w'=>'week',
  66. 'd'=>'day',
  67. 'h'=>'hour'
  68. ];
  69. $kept_diff=array_intersect_key((array)$diff,$units); // omit unwanted elements of diff()'s output
  70. if(!max($kept_diff)){ // if y, m, w, d, & h all = 0, show default
  71. return 'default: 1 hour ago'; // inserted "default: " for demo. Remove in production.
  72. }else{
  73. $diffs=array_filter(array_merge($units,$kept_diff)); // sort by $units order, then omit elements with 0 value
  74. return current($diffs).' '.$units[key($diffs)].(current($diffs)>1?'s':'').' ago'; // return highest unit data in plain English
  75. }
  76. }
  77. echo time_elapsed_string('2017-06-30 02:22:35'); // output: 3 weeks ago
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement