You are here

function facetapi_get_timestamp_gap in Facet API 7

Same name and namespace in other branches
  1. 6.3 facetapi.date.inc \facetapi_get_timestamp_gap()
  2. 7.2 facetapi.date.inc \facetapi_get_timestamp_gap()

Determines the best search gap to use for an arbitrary date range.

Generally, we use the maximum gap that fits between the start and end date. If they are more than a year apart, 1 year; if they are more than a month apart, 1 month; etc.

This function uses Unix timestamps for its computation and so is not useful for dates outside that range.

Parameters

int $start_time: A string containing the start date as an ISO date string.

int $end_time: A string containing the end date as an ISO date string.

string|NULL $min_gap: (Optional) The minimum gap that should be returned.

Return value

string A string containing the gap, see FACETAPI_DATE_* constants for valid values. Returns FALSE of either of the dates cannot be converted to a timestamp.

1 call to facetapi_get_timestamp_gap()
facetapi_get_date_gap in ./facetapi.date.inc
Converts ISO date strings to Unix timestamps, passes values to the facetapi_get_timestamp_gap() function to calculate the gap.

File

./facetapi.date.inc, line 109
Date handling functions.

Code

function facetapi_get_timestamp_gap($start_time, $end_time, $min_gap = NULL) {
  $time_diff = $end_time - $start_time;
  switch (TRUE) {

    // NOTE: 31536000 == 60 * 60 * 24 * 365
    case $time_diff >= 31536000:
      $gap = FACETAPI_DATE_YEAR;
      break;
    case $time_diff >= 86400 * gmdate('t', $start_time):
      $gap = FACETAPI_DATE_MONTH;
      break;
    case $time_diff >= 86400:
      $gap = FACETAPI_DATE_DAY;
      break;
    case $time_diff >= 3600:
      $gap = FACETAPI_DATE_HOUR;
      break;
    case $time_diff >= 60:
      $gap = FACETAPI_DATE_MINUTE;
      break;
    default:
      $gap = FACETAPI_DATE_SECOND;
      break;
  }

  // Return the calculated gap if a minimum gap was not passed of the calculated
  // gap is a larger interval than the minimum gap.
  if (null === $min_gap || facetapi_gap_compare($gap, $min_gap) >= 0) {
    return $gap;
  }
  else {
    return $min_gap;
  }
}