You are here

function date_sql_handler::sql_date_math in Date 7

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

Adjust a field value by time interval.

Parameters

$field: The field to be adjusted.

$direction: Either ADD or SUB.

$count: The number of values to adjust.

$granularity: The granularity of the adjustment, should be singular, like SECOND, MINUTE, DAY, HOUR.

File

date_api/date_api_sql.inc, line 234
SQL helper for Date API.

Class

date_sql_handler
A class to manipulate date SQL.

Code

function sql_date_math($field, $direction, $count, $granularity) {
  $granularity = strtoupper($granularity);
  switch ($this->db_type) {
    case 'mysql':
    case 'mysqli':
      switch ($direction) {
        case 'ADD':
          return "DATE_ADD({$field}, INTERVAL {$count} {$granularity})";
        case 'SUB':
          return "DATE_SUB({$field}, INTERVAL {$count} {$granularity})";
      }
    case 'pgsql':
      $granularity .= 'S';
      switch ($direction) {
        case 'ADD':
          return "({$field} + INTERVAL '{$count} {$granularity}')";
        case 'SUB':
          return "({$field} - INTERVAL '{$count} {$granularity}')";
      }
  }
  return $field;
}