You are here

function DateSqlHandler::arg_replace in Date 8

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

File

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

Class

DateSqlHandler
A class to manipulate date SQL.

Namespace

Drupal\date_api

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',
    '  ' => '',
    ' ' => '',
  );
  $prefix = in_array($item, array(
    'hours',
    'hour',
    'minutes',
    'minute',
    'seconds',
    'second',
  )) ? 'T' : '';
  return $prefix . strtr($direction, $replace) . $count . strtr($item, $replace);
}