You are here

function date_sql_handler::arg_parts in Date 5.2

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

Parse date parts from an ISO date argument.

Based on ISO 8601 date duration and time interval standards.

See http://en.wikipedia.org/wiki/ISO_8601#Week_dates for definitions of ISO weeks. See http://en.wikipedia.org/wiki/ISO_8601#Duration for definitions of ISO duration and time interval.

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

The 'to' portion of the argument can be eliminated if it is the same as the 'from' 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.

2 calls to date_sql_handler::arg_parts()
date_sql_handler::arg_granularity in ./date_api_sql.inc
Use the parsed values from the ISO argument to determine the granularity of this period.
date_sql_handler::arg_range in ./date_api_sql.inc
Use the parsed values from the ISO argument to determine the min and max date for this period.

File

./date_api_sql.inc, line 722

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