You are here

function facetapi_get_timestamp_gap in Facet API 6.3

Same name and namespace in other branches
  1. 7.2 facetapi.date.inc \facetapi_get_timestamp_gap()
  2. 7 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

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

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

Return value

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 107
Date handling functions.

Code

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

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