A modest collection of PHP libraries used at SparkFun.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

178 lines
5.0 KiB

  1. <?php
  2. namespace SparkLib\Util;
  3. /**
  4. * A place to hang date-and-time stuff that doesn't seem
  5. * to exist elsewhere. Considerable redundancy at the
  6. * moment; liable to change substantially.
  7. */
  8. class DateTime {
  9. public static function format ($timestamp, $format = 'Y-m-d')
  10. {
  11. if (is_string($timestamp))
  12. $timestamp = strtotime($timestamp);
  13. return date($format, $timestamp);
  14. }
  15. public static function formatDefaultTimeAndDate ($date) {
  16. return static::format($date, 'Y-m-d G:i');
  17. }
  18. public static function interval_since ($date)
  19. {
  20. $iv = date_diff(
  21. new \DateTime($date),
  22. new \DateTime('now')
  23. );
  24. $units = array(
  25. ' yr ' => $iv->y,
  26. ' mo ' => $iv->m,
  27. ' dy ' => $iv->d,
  28. ' hr ' => $iv->h,
  29. ' min' => $iv->i,
  30. );
  31. $str = '';
  32. $found = 0;
  33. foreach ($units as $label => $val) {
  34. if ($val > 0)
  35. $found++;
  36. if ($found > 0)
  37. $str .= $val . $label;
  38. if ($found > 2)
  39. break;
  40. }
  41. return $str;
  42. }
  43. public static function interval ($seconds)
  44. {
  45. // This is pretty silly.
  46. $str = '';
  47. $segment = function ($divisor, $label) use (&$seconds, &$str) {
  48. $units = round(($seconds - ($seconds % $divisor)) / $divisor);
  49. $seconds = $seconds % $divisor;
  50. if ($units)
  51. $str .= "$units $label ";
  52. };
  53. $segment(86400, 'dy');
  54. $segment(3600, 'hr');
  55. $segment(60, 'min');
  56. return trim($str . ($seconds ? "$seconds sec" : ''));
  57. }
  58. // heh. fuzzy interval.
  59. // Stolen from the internet.
  60. // http://blog.slapthink.net/2009/04/16/fuzzy-time-or-relative-time-in-php/
  61. public static function fuzzy_interval ($date_from)
  62. {
  63. $_time_formats = array(
  64. array(60, 'just now'),
  65. array(90, '1 minute'),
  66. array(3600, 'minutes', 60),
  67. array(5400, '1 hour'),
  68. array(86400, 'hours', 3600),
  69. array(129600, '1 day'),
  70. array(604800, 'days', 86400),
  71. array(907200, '1 week'),
  72. array(2628000, 'weeks', 604800),
  73. array(3942000, '1 month'),
  74. array(31536000, 'months', 2628000),
  75. array(47304000, '1 year'),
  76. array(3153600000, 'years', 31536000),
  77. );
  78. $now = time();// current unix timestamp
  79. // if a number is passed assume it is a unix time stamp
  80. // if string is passed try and parse it to unix time stamp
  81. if(is_numeric($date_from)){
  82. $dateFrom = $date_from;
  83. }elseif (is_string($date_from)) {
  84. $dateFrom = strtotime($date_from);
  85. }
  86. $difference = $now - $dateFrom;// difference between now and the passed time.
  87. $val = '';// value to return
  88. if ($dateFrom <= 0) {
  89. $val = 'a long time ago';
  90. } else {
  91. //loop through each format measurement in array
  92. foreach ($_time_formats as $format) {
  93. // if the difference from now and passed time is less than first option in format measurment
  94. if ($difference < $format[0]) {
  95. //if the format array item has no calculation value
  96. if (count($format) == 2) {
  97. $val = $format[1] . ($format[0] === 60 ? '' : ' ago');
  98. break;
  99. } else {
  100. // divide difference by format item value to get number of units
  101. $val = ceil($difference / $format[2]) . ' ' . $format[1] . ' ago';
  102. break;
  103. }
  104. }
  105. }
  106. }
  107. return $val;
  108. }
  109. /**
  110. * from the internets
  111. *
  112. * @param time() $small_ts
  113. * @param time() $large_ts
  114. */
  115. public static function contextualTime ($small_ts, $large_ts=false)
  116. {
  117. if (! $large_ts) {
  118. $large_ts = time();
  119. }
  120. $n = $large_ts - $small_ts;
  121. if ($n <= 1)
  122. return 'less than 1 second ago';
  123. if ($n < (60))
  124. return $n . ' seconds ago';
  125. if ($n < (60*60)) {
  126. $minutes = round($n/60);
  127. return 'about ' . $minutes . ' minute' . ($minutes > 1 ? 's' : '') . ' ago';
  128. }
  129. if ($n < (60*60*16)) {
  130. $hours = round($n/(60*60));
  131. return 'about ' . $hours . ' hour' . ($hours > 1 ? 's' : '') . ' ago';
  132. }
  133. if ($n < (time() - strtotime('yesterday')))
  134. return 'yesterday';
  135. if ($n < (60*60*24)) {
  136. $hours = round($n/(60*60));
  137. return 'about ' . $hours . ' hour' . ($hours > 1 ? 's' : '') . ' ago';
  138. }
  139. if ($n < (60*60*24*6.5)) return 'about ' . round($n/(60*60*24)) . ' days ago';
  140. if ($n < (time() - strtotime('last week'))) return 'last week';
  141. if (round($n/(60*60*24*7)) == 1) return 'about a week ago';
  142. if ($n < (60*60*24*7*3.5)) return 'about ' . round($n/(60*60*24*7)) . ' weeks ago';
  143. if ($n < (time() - strtotime('last month'))) return 'last month';
  144. if (round($n/(60*60*24*7*4)) == 1) return 'about a month ago';
  145. if ($n < (60*60*24*7*4*11.5)) return 'about ' . round($n/(60*60*24*7*4)) . ' months ago';
  146. if ($n < (time() - strtotime('last year'))) return 'last year';
  147. if (round($n/(60*60*24*7*52)) == 1) return 'about a year ago';
  148. if ($n >= (60*60*24*7*4*12)) return 'about ' . round($n/(60*60*24*7*52)) . ' years ago';
  149. return false;
  150. }
  151. }