1 date_sql_handler.inc date_sql_handler::arg_replace($arg)

Convert strings like '+1 day' to the ISO equivalent, like 'P1D' .

File

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

Class

date_sql_handler
A class to manipulate date SQL.

Code

function arg_replace($arg) {
  if (!preg_match('/([+|-])\s?([0-9]{1,32})\s?([day(s)?|week(s)?|month(s)?|year(s)?|hour(s)?|minute(s)?|second(s)?]{1,10})/', $arg, $results)) {
    return str_replace('now', '@', $arg);
  }
  $direction = $results[1];
  $count = $results[2];
  $item = $results[3];
  $replace = array(
    'now' => '@',
    '+' => 'P',
    '-' => 'P-',
    'years' => 'Y',
    'year' => 'Y',
    'months' => 'M',
    'month' => 'M',
    'weeks' => 'W',
    'week' => 'W',
    'days' => 'D',
    'day' => 'D',
    'hours' => 'H',
    'hour' => 'H',
    'minutes' => 'M',
    'minute' => 'M',
    'seconds' => 'S',
    'second' => 'S',
    '  ' => '',
    ' ' => '',
  );
  $args = array('hours', 'hour', 'minutes', 'minute', 'seconds', 'second');
  if (in_array($item, $args)) {
    $prefix = 'T';
  }
  else {
    $prefix = '';
  }
  $return = $prefix;
  $return .= strtr($direction, $replace);
  $return .= $count;
  $return .= strtr($item, $replace);

  return $return;
}