You are here

function date_sql_handler::arg_range in Date 5.2

Same name and namespace in other branches
  1. 6.2 date_api_sql.inc \date_sql_handler::arg_range()
  2. 6 date_api_sql.inc \date_sql_handler::arg_range()
  3. 7.3 date_api/date_api_sql.inc \date_sql_handler::arg_range()
  4. 7 date_api/date_api_sql.inc \date_sql_handler::arg_range()
  5. 7.2 date_api/date_api_sql.inc \date_sql_handler::arg_range()

Use the parsed values from the ISO argument to determine the min and max date for this period.

File

./date_api_sql.inc, line 821

Class

date_sql_handler
A class to manipulate date SQL.

Code

function arg_range($arg) {

  // Parse the argument to get its parts
  $parts = $this
    ->arg_parts($arg);

  // Build a range from a period-only argument (assumes the min date is now.)
  if (empty($parts[0]['date']) && !empty($parts[0]['period']) && empty($parts[1])) {
    $min_date = date_now();
    $max_date = drupal_clone($min_date);
    foreach ($parts[0]['period'] as $part => $value) {
      date_modify($max_date, "+{$value} {$part}");
    }
    date_modify($max_date, '-1 second');
    return array(
      $min_date,
      $max_date,
    );
  }

  // Build a range from a period to period argument
  if (empty($parts[0]['date']) && !empty($parts[0]['period']) && !empty($parts[1]['period'])) {
    $min_date = date_now();
    $max_date = drupal_clone($min_date);
    foreach ($parts[0]['period'] as $part => $value) {
      date_modify($min_date, "+{$value} {$part}");
    }
    date_modify($min_date, '-1 second');
    foreach ($parts[1]['period'] as $part => $value) {
      date_modify($max_date, "+{$value} {$part}");
    }
    date_modify($max_date, '-1 second');
    return array(
      $min_date,
      $max_date,
    );
  }
  if (!empty($parts[0]['date'])) {
    $value = date_fuzzy_datetime($this
      ->complete_date($parts[0]['date'], 'min'));
    $min_date = date_make_date($value, date_default_timezone_name(), DATE_ISO);

    // Build a range from a single date-only argument.
    if (empty($parts[1]) || empty($parts[1]['date']) && empty($parts[1]['period'])) {
      $value = date_fuzzy_datetime($this
        ->complete_date($parts[0]['date'], 'max'));
      $max_date = date_make_date($value, date_default_timezone_name(), DATE_ISO);
      return array(
        $min_date,
        $max_date,
      );
    }
    elseif (!empty($parts[1]['period'])) {
      foreach ($parts[1]['period'] as $part => $value) {
        $max_date = drupal_clone($min_date);
        date_modify($max_date, "+{$value} {$part}");
      }
      date_modify($max_date, '-1 second');
      return array(
        $min_date,
        $max_date,
      );
    }
  }

  // Build a range from start date and end date.
  if (!empty($parts[1]['date'])) {
    $value = date_fuzzy_datetime($this
      ->complete_date($parts[1]['date'], 'max'));
    $max_date = date_make_date($value, date_default_timezone_name(), DATE_ISO);
    if (isset($min_date)) {
      return array(
        $min_date,
        $max_date,
      );
    }
  }

  // Build a range from period + end date.
  if (!empty($parts[0]['period'])) {
    $min_date = date_now();
    foreach ($parts[0]['period'] as $part => $value) {
      date_modify($min_date, "{$value} {$part}");
    }
    return array(
      $min_date,
      $max_date,
    );
  }

  // Intercept invalid info and fall back to the current date.
  $now = date_now();
  return array(
    $now,
    $now,
  );
}