You are here

function date_sql_handler::arg_range in Date 7

Same name and namespace in other branches
  1. 5.2 date_api_sql.inc \date_sql_handler::arg_range()
  2. 6.2 date_api_sql.inc \date_sql_handler::arg_range()
  3. 6 date_api_sql.inc \date_sql_handler::arg_range()
  4. 7.3 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/date_api_sql.inc, line 821
SQL helper for Date API.

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 = 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 = 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 = $this
      ->complete_date($parts[0]['date'], 'min');
    $min_date = new DateObject($value, date_default_timezone(), DATE_FORMAT_DATETIME);

    // Build a range from a single date-only argument.
    if (empty($parts[1]) || empty($parts[1]['date']) && empty($parts[1]['period'])) {
      $value = $this
        ->complete_date($parts[0]['date'], 'max');
      $max_date = new DateObject($value, date_default_timezone(), DATE_FORMAT_DATETIME);
      return array(
        $min_date,
        $max_date,
      );
    }
    elseif (!empty($parts[1]['period'])) {
      foreach ($parts[1]['period'] as $part => $value) {
        $max_date = 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 = $this
      ->complete_date($parts[1]['date'], 'max');
    $max_date = new DateObject($value, date_default_timezone(), DATE_FORMAT_DATETIME);
    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,
  );
}