timeSince() PHP function


/ Published in: PHP
Save to your folder(s)

Get time difference in human readable format


Copy this code and paste it in your HTML
  1. function timeBetween($start,$end) {
  2. if (empty($start) || empty($end)) { return; }
  3. else return timeSince(strtotime($start),strtotime($end),2,'short');
  4. }
  5.  
  6. /**
  7. * Get time difference in human readable format
  8. * $start is start time (unix time), $end is end time (unix time), $units is numeric and defines how many time units to display
  9. */
  10. function timeSince($start,$end='',$units=2,$words='long') { // $start and $end should be Unix time() format
  11. switch($words) {
  12. case 'long':
  13. $unitName = array( // Change this array to reflect what should be displayed as unit names and pluralization append
  14. 'year' => 'year',
  15. 'month' => 'month',
  16. 'week' => 'week',
  17. 'day' => 'day',
  18. 'hour' => 'hour',
  19. 'minute' => 'minute',
  20. 'second' => 'second',
  21. 'plural' => 's'
  22. );
  23. break;
  24. case 'short':
  25. $unitName = array( // Change this array to reflect what should be displayed as unit names and pluralization append
  26. 'year' => 'yr',
  27. 'month' => 'mo',
  28. 'week' => 'wk',
  29. 'day' => 'day',
  30. 'hour' => 'hr',
  31. 'minute' => 'min',
  32. 'second' => 'sec',
  33. 'plural' => 's'
  34. );
  35. break;
  36. }
  37. // Common time periods as an array of arrays
  38. $periods = array(
  39. array(60 * 60 * 24 * 365 , $unitName['year']),
  40. array(60 * 60 * 24 * 30 , $unitName['month']),
  41. array(60 * 60 * 24 * 7, $unitName['week']),
  42. array(60 * 60 * 24 , $unitName['day']),
  43. array(60 * 60 , $unitName['hour']),
  44. array(60 , $unitName['minute']),
  45. array(1 , $unitName['second']),
  46. );
  47. $end = (!empty($end))?$end:time(); // if no end timestamp given use the current one for end date
  48. $since = $end - $start; // Find the difference of time between now and the past
  49. // Loop around the periods, starting with the biggest
  50. for ($i = 0, $j = count($periods); $i < $j; $i++){
  51. $seconds = $periods[$i][0];
  52. $name = $periods[$i][1];
  53. // Find the biggest whole period
  54. if (($count = floor($since / $seconds)) != 0){
  55. break;
  56. }
  57. }
  58. $output = ($count == 1) ? '1 '.$name : "$count {$name}s";
  59. $deducted = ($seconds * $count);
  60. for($z = 1, $j = count($periods); $z < $j; $z++) {
  61. if ($units > $z && $i + $z < $j){
  62. // Retrieving the next requested relevant period
  63. $seconds = $periods[$i + $z][0];
  64. $name = $periods[$i + $z][1];
  65. // Only show it if it's greater than 0
  66. if (($count = floor(($since - $deducted) / $seconds)) != 0){
  67. $deducted = $deducted+($seconds * $count);
  68. $output .= ($count == 1) ? ', 1 '.$name : ", {$count} {$name}{$unitName['plural']}";
  69. }
  70. }
  71. }
  72. return $output;
  73. }
  74. /**
  75. * Identical to timeSince except your end time should be greater than your start time
  76. */
  77. function timeUntil($end,$start='',$units=2) {
  78. $start = (!empty($start))?$start:time();
  79. return timeSince($start,$end,$units);
  80. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.