You are here

function DateSqlHandler::arg_range in Date 8

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

File

date_api/lib/Drupal/date_api/DateSqlHandler.php, line 1027

Class

DateSqlHandler
A class to manipulate date SQL.

Namespace

Drupal\date_api

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 = new DrupalDateTime();
    $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 = new DrupalDateTime();
    $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 DrupalDateTime($value, drupal_get_user_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 DrupalDateTime($value, drupal_get_user_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 DrupalDateTime($value, drupal_get_user_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 = new DrupalDateTime();
    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 = new DrupalDateTime();
  return array(
    $now,
    $now,
  );
}