1 date_sql_handler.inc date_sql_handler::arg_parts($argument)

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

File

core/modules/date/views/date_sql_handler.inc, line 708
SQL helper for Date API.

Class

date_sql_handler
A class to manipulate date SQL.

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 = date_now();
      $parts['date'] = $date->toArray();
    }
    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;
}