You are here

function date_sql_handler::arg_replace in Date 5.2

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

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

File

./date_api_sql.inc, line 770

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