facetapi.date.inc 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <?php
  2. /**
  3. * @file
  4. * Date handling functions.
  5. */
  6. /**
  7. * Helper function to convert dates from Unix timestamps into ISO 8601 format.
  8. *
  9. * @param $timestamp
  10. * An integer containing the Unix timestamp being converted.
  11. * @param $gap
  12. * A string containing the gap, see FACETAPI_DATE_* constants for valid
  13. * values. Defaults to FACETAPI_DATE_SECOND.
  14. *
  15. * @return
  16. * A string containing the date in ISO 8601 format.
  17. */
  18. function facetapi_isodate($timestamp, $gap = FACETAPI_DATE_SECOND) {
  19. switch ($gap) {
  20. case FACETAPI_DATE_SECOND:
  21. $format = FACETAPI_DATE_ISO8601;
  22. break;
  23. case FACETAPI_DATE_MINUTE:
  24. $format = 'Y-m-d\TH:i:00\Z';
  25. break;
  26. case FACETAPI_DATE_HOUR:
  27. $format = 'Y-m-d\TH:00:00\Z';
  28. break;
  29. case FACETAPI_DATE_DAY:
  30. $format = 'Y-m-d\T00:00:00\Z';
  31. break;
  32. case FACETAPI_DATE_MONTH:
  33. $format = 'Y-m-01\T00:00:00\Z';
  34. break;
  35. case FACETAPI_DATE_YEAR:
  36. $format = 'Y-01-01\T00:00:00\Z';
  37. break;
  38. default:
  39. $format = FACETAPI_DATE_ISO8601;
  40. break;
  41. }
  42. return gmdate($format, $timestamp);
  43. }
  44. /**
  45. * Return a date gap one increment smaller than the one passed.
  46. *
  47. * @param $gap
  48. * A string containing the gap, see FACETAPI_DATE_* constants for valid
  49. * values.
  50. * @param $min_gap
  51. * A string containing the the minimum gap that can be returned, defaults to
  52. * FACETAPI_DATE_SECOND. This is useful for defining the smallest increment
  53. * that can be used in a date drilldown.
  54. *
  55. * @return
  56. * A string containing the smaller date gap, NULL if there is no smaller gap.
  57. * See FACETAPI_DATE_* constants for valid values.
  58. */
  59. function facetapi_get_next_date_gap($gap, $min_gap = FACETAPI_DATE_SECOND) {
  60. // Array of numbers used to determine whether the next gap is smaller than
  61. // the minimum gap allowed in the drilldown.
  62. $gap_numbers = array(
  63. FACETAPI_DATE_YEAR => 6,
  64. FACETAPI_DATE_MONTH => 5,
  65. FACETAPI_DATE_DAY => 4,
  66. FACETAPI_DATE_HOUR => 3,
  67. FACETAPI_DATE_MINUTE => 2,
  68. FACETAPI_DATE_SECOND => 1,
  69. );
  70. // Gets gap numbers for both the gap and minimum gap, checks if the next gap
  71. // is within the limit set by the $min_gap parameter.
  72. $gap_num = isset($gap_numbers[$gap]) ? $gap_numbers[$gap] : 6;
  73. $min_num = isset($gap_numbers[$min_gap]) ? $gap_numbers[$min_gap] : 1;
  74. return ($gap_num > $min_num) ? array_search($gap_num - 1, $gap_numbers) : $min_gap;
  75. }
  76. /**
  77. * Determines the best search gap to use for an arbitrary date range.
  78. *
  79. * Generally, we use the maximum gap that fits between the start and end date.
  80. * If they are more than a year apart, 1 year; if they are more than a month
  81. * apart, 1 month; etc.
  82. *
  83. * This function uses Unix timestamps for its computation and so is not useful
  84. * for dates outside that range.
  85. *
  86. * @param int $start_time
  87. * A string containing the start date as an ISO date string.
  88. * @param int $end_time
  89. * A string containing the end date as an ISO date string.
  90. * @param string|NULL $min_gap
  91. * (Optional) The minimum gap that should be returned.
  92. *
  93. * @return string
  94. * A string containing the gap, see FACETAPI_DATE_* constants for valid
  95. * values. Returns FALSE of either of the dates cannot be converted to a
  96. * timestamp.
  97. */
  98. function facetapi_get_timestamp_gap($start_time, $end_time, $min_gap = NULL) {
  99. $time_diff = $end_time - $start_time;
  100. switch (TRUE) {
  101. // NOTE: 31536000 == 60 * 60 * 24 * 365
  102. case ($time_diff >= 31536000):
  103. $gap = FACETAPI_DATE_YEAR;
  104. break;
  105. case ($time_diff >= 86400 * gmdate('t', $start_time)):
  106. $gap = FACETAPI_DATE_MONTH;
  107. break;
  108. case ($time_diff >= 86400):
  109. $gap = FACETAPI_DATE_DAY;
  110. break;
  111. case ($time_diff >= 3600):
  112. $gap = FACETAPI_DATE_HOUR;
  113. break;
  114. case ($time_diff >= 60):
  115. $gap = FACETAPI_DATE_MINUTE;
  116. break;
  117. default:
  118. $gap = FACETAPI_DATE_SECOND;
  119. break;
  120. }
  121. // Return the calculated gap if a minimum gap was not passed of the calculated
  122. // gap is a larger interval than the minimum gap.
  123. if (null === $min_gap || facetapi_gap_compare($gap, $min_gap) >= 0) {
  124. return $gap;
  125. }
  126. else {
  127. return $min_gap;
  128. }
  129. }
  130. /**
  131. * Converts ISO date strings to Unix timestamps, passes values to the
  132. * facetapi_get_timestamp_gap() function to calculate the gap.
  133. *
  134. * @param $start_date
  135. * A string containing the start date as an ISO date string.
  136. * @param $end_date
  137. * A string containing the end date as an ISO date string.
  138. * @param string|NULL $min_gap
  139. * (Optional) The minimum gap that should be returned.
  140. *
  141. * @return string
  142. * A string containing the gap, see FACETAPI_DATE_* constants for valid
  143. * values. Returns FALSE of either of the dates cannot be converted to a
  144. * timestamp.
  145. *
  146. * @see facetapi_get_timestamp_gap()
  147. */
  148. function facetapi_get_date_gap($start_date, $end_date, $min_gap = NULL) {
  149. $range = array(strtotime($start_date), strtotime($end_date));
  150. if (!in_array(FALSE, $range, TRUE)) {
  151. return facetapi_get_timestamp_gap($range[0], $range[1], $min_gap);
  152. }
  153. return FALSE;
  154. }
  155. /**
  156. * Returns a formatted date based on the passed timestamp and gap.
  157. *
  158. * This function assumes that gaps less than one day will be displayed in a
  159. * search context in which a larger containing gap including a day is already
  160. * displayed. So, HOUR, MINUTE, and SECOND gaps only display time information,
  161. * without date.
  162. *
  163. * @param $timestamp
  164. * An integer containing the Unix timestamp.
  165. * @param $gap
  166. * A string containing the gap, see FACETAPI_DATE_* constants for valid
  167. * values, defaults to YEAR.
  168. *
  169. * @return
  170. * A gap-appropriate display date used in the facet link.
  171. */
  172. function facetapi_format_timestamp($timestamp, $gap = FACETAPI_DATE_YEAR) {
  173. switch ($gap) {
  174. case FACETAPI_DATE_MONTH:
  175. return format_date($timestamp, 'custom', 'F Y', 'UTC');
  176. case FACETAPI_DATE_DAY:
  177. return format_date($timestamp, 'custom', 'F j, Y', 'UTC');
  178. case FACETAPI_DATE_HOUR:
  179. return format_date($timestamp, 'custom', 'g A', 'UTC');
  180. case FACETAPI_DATE_MINUTE:
  181. return format_date($timestamp, 'custom', 'g:i A', 'UTC');
  182. case FACETAPI_DATE_SECOND:
  183. return format_date($timestamp, 'custom', 'g:i:s A', 'UTC');
  184. default:
  185. return format_date($timestamp, 'custom', 'Y', 'UTC');
  186. }
  187. }
  188. /**
  189. * Returns a formatted date based on the passed ISO date string and gap.
  190. *
  191. * @param $date
  192. * A string containing the date as an ISO date string.
  193. * @param $gap
  194. * A string containing the gap, see FACETAPI_DATE_* constants for valid
  195. * values, defaults to YEAR.
  196. * @param $callback
  197. * The formatting callback, defaults to "facetapi_format_timestamp".
  198. *
  199. * @return
  200. * A gap-appropriate display date used in the facet link.
  201. *
  202. * @see facetapi_format_timestamp()
  203. */
  204. function facetapi_format_date($date, $gap = FACETAPI_DATE_YEAR, $callback = 'facetapi_format_timestamp') {
  205. $timestamp = strtotime($date);
  206. return $callback($timestamp, $gap);
  207. }
  208. /**
  209. * Returns the next increment from the given ISO date and gap. This function is
  210. * useful for getting the upper limit of a date range from the given start
  211. * date.
  212. *
  213. * @param $date
  214. * A string containing the date as an ISO date string.
  215. * @param $gap
  216. * A string containing the gap, see FACETAPI_DATE_* constants for valid
  217. * values, defaults to YEAR.
  218. *
  219. * @return
  220. * A string containing the date, FALSE if the passed date could not be parsed.
  221. */
  222. function facetapi_get_next_date_increment($date, $gap) {
  223. if (preg_match(FACETAPI_REGEX_DATE, $date, $match)) {
  224. // Increments the timestamp.
  225. switch ($gap) {
  226. case FACETAPI_DATE_MONTH:
  227. $match[2] += 1;
  228. break;
  229. case FACETAPI_DATE_DAY:
  230. $match[3] += 1;
  231. break;
  232. case FACETAPI_DATE_HOUR:
  233. $match[4] += 1;
  234. break;
  235. case FACETAPI_DATE_MINUTE:
  236. $match[5] += 1;
  237. break;
  238. case FACETAPI_DATE_SECOND:
  239. $match[6] += 1;
  240. break;
  241. default:
  242. $match[1] += 1;
  243. break;
  244. }
  245. // Gets the next incremenet.
  246. return facetapi_isodate(
  247. gmmktime($match[4], $match[5], $match[6], $match[2], $match[3], $match[1])
  248. );
  249. }
  250. return FALSE;
  251. }
  252. /**
  253. * Compares two timestamp gaps.
  254. *
  255. * @param string $gap1
  256. * @param string $gap2
  257. *
  258. * @return int
  259. * Returns -1 if gap1 is less than gap2, 1 if gap1 is greater than gap2, and 0
  260. * if they are equal.
  261. */
  262. function facetapi_gap_compare($gap1, $gap2) {
  263. $gap_numbers = array(
  264. FACETAPI_DATE_YEAR => 6,
  265. FACETAPI_DATE_MONTH => 5,
  266. FACETAPI_DATE_DAY => 4,
  267. FACETAPI_DATE_HOUR => 3,
  268. FACETAPI_DATE_MINUTE => 2,
  269. FACETAPI_DATE_SECOND => 1,
  270. );
  271. $gap1_num = isset($gap_numbers[$gap1]) ? $gap_numbers[$gap1] : 6;
  272. $gap2_num = isset($gap_numbers[$gap2]) ? $gap_numbers[$gap2] : 6;
  273. if ($gap1_num == $gap2_num) {
  274. return 0;
  275. }
  276. else {
  277. return ($gap1_num < $gap2_num) ? -1 : 1;
  278. }
  279. }