You are here

public function FacetsDateHandler::getTimestampGap in Facets 8

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 FACETS_DATE_* constants for valid values. Returns FALSE of either of the dates cannot be converted to a timestamp.

1 call to FacetsDateHandler::getTimestampGap()
FacetsDateHandler::getDateGap in src/Utility/FacetsDateHandler.php
Converts ISO date strings to Unix timestamps.

File

src/Utility/FacetsDateHandler.php, line 180

Class

FacetsDateHandler
Dates Handler service.

Namespace

Drupal\facets\Utility

Code

public function getTimestampGap($start_time, $end_time, $min_gap = NULL) {
  $time_diff = $end_time - $start_time;
  switch (TRUE) {
    case $time_diff >= 31536000:
      $gap = static::FACETS_DATE_YEAR;
      break;
    case $time_diff >= 86400 * gmdate('t', $start_time):
      $gap = static::FACETS_DATE_MONTH;
      break;
    case $time_diff >= 86400:
      $gap = static::FACETS_DATE_DAY;
      break;
    case $time_diff >= 3600:
      $gap = static::FACETS_DATE_HOUR;
      break;
    case $time_diff >= 60:
      $gap = static::FACETS_DATE_MINUTE;
      break;
    default:
      $gap = static::FACETS_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 (is_null($min_gap) || $this
    ->gapCompare($gap, $min_gap) >= 0) {
    return $gap;
  }
  else {
    return $min_gap;
  }
}