You are here

function DateSqlHandler::arg_parts in Date 8

Parse date parts from an ISO date argument.

Based on ISO 8601 date duration and time interval standards.

Parses a value like 2006-01-01--2006-01-15, or 2006-W24, or @P1W. Separate start and end dates or date and period with a double hyphen (--).

The 'end' portion of the argument can be eliminated if it is the same as the 'start' portion. Use @ instead of a date to substitute in the current date and time.

Use periods (P1H, P1D, P1W, P1M, P1Y) to get next hour/day/week/month/year from now. Use date before P sign to get next hour/day/week/month/year from that date. Use period then date to get a period that ends on the date.

See also

http://en.wikipedia.org/wiki/ISO_8601#Week_dates

http://en.wikipedia.org/wiki/ISO_8601#Duration

2 calls to DateSqlHandler::arg_parts()
DateSqlHandler::arg_granularity in date_api/lib/Drupal/date_api/DateSqlHandler.php
Use the parsed values from the ISO argument to determine the granularity of this period.
DateSqlHandler::arg_range in date_api/lib/Drupal/date_api/DateSqlHandler.php
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 903

Class

DateSqlHandler
A class to manipulate date SQL.

Namespace

Drupal\date_api

Code

function arg_parts($argument) {
  $values = array();

  // Keep mal-formed arguments from creating errors.
  if (empty($argument) || is_array($argument)) {
    return array(
      'date' => array(),
      'period' => array(),
    );
  }
  $fromto = explode('--', $argument);
  foreach ($fromto as $arg) {
    $parts = array();
    if ($arg == '@') {
      $date = new DrupalDateTime();
      $parts['date'] = date_parse($date
        ->format(DATE_FORMAT_DATETIME));
    }
    elseif (preg_match('/(\\d{4})?-?(W)?(\\d{1,2})?-?(\\d{1,2})?[T\\s]?(\\d{1,2})?:?(\\d{1,2})?:?(\\d{1,2})?/', $arg, $matches)) {
      $date = array();
      if (!empty($matches[1])) {
        $date['year'] = $matches[1];
      }
      if (!empty($matches[3])) {
        if (empty($matches[2])) {
          $date['month'] = $matches[3];
        }
        else {
          $date['week'] = $matches[3];
        }
      }
      if (!empty($matches[4])) {
        $date['day'] = $matches[4];
      }
      if (!empty($matches[5])) {
        $date['hour'] = $matches[5];
      }
      if (!empty($matches[6])) {
        $date['minute'] = $matches[6];
      }
      if (!empty($matches[7])) {
        $date['second'] = $matches[7];
      }
      $parts['date'] = $date;
    }
    if (preg_match('/^P(\\d{1,4}[Y])?(\\d{1,2}[M])?(\\d{1,2}[W])?(\\d{1,2}[D])?([T]{0,1})?(\\d{1,2}[H])?(\\d{1,2}[M])?(\\d{1,2}[S])?/', $arg, $matches)) {
      $period = array();
      if (!empty($matches[1])) {
        $period['year'] = str_replace('Y', '', $matches[1]);
      }
      if (!empty($matches[2])) {
        $period['month'] = str_replace('M', '', $matches[2]);
      }
      if (!empty($matches[3])) {
        $period['week'] = str_replace('W', '', $matches[3]);
      }
      if (!empty($matches[4])) {
        $period['day'] = str_replace('D', '', $matches[4]);
      }
      if (!empty($matches[6])) {
        $period['hour'] = str_replace('H', '', $matches[6]);
      }
      if (!empty($matches[7])) {
        $period['minute'] = str_replace('M', '', $matches[7]);
      }
      if (!empty($matches[8])) {
        $period['second'] = str_replace('S', '', $matches[8]);
      }
      $parts['period'] = $period;
    }
    $values[] = $parts;
  }
  return $values;
}