You are here

function date_sql_handler::sql_date_math in Date 6.2

Same name and namespace in other branches
  1. 5.2 date_api_sql.inc \date_sql_handler::sql_date_math()
  2. 7.3 date_api/date_api_sql.inc \date_sql_handler::sql_date_math()
  3. 7 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_sql.inc, line 241
SQL date functions.

Class

date_sql_handler
A class to manipulate date SQL.

Code

function sql_date_math($field, $direction, $count, $granularity) {
  $granularity = drupal_strtoupper($granularity);
  switch ($this->db_type) {
    case 'mysql':
    case 'mysqli':
      switch ($direction) {
        case 'ADD':
          return "DATE_ADD(CAST({$field} AS DATETIME), INTERVAL {$count} {$granularity})";
        case 'SUB':
          return "DATE_SUB(CAST({$field} AS DATETIME), 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;
}