You are here

function DateSqlHandler::sql_date_math in Date 8

Adjusts a field value by time interval.

Parameters

string $field: The field to be adjusted.

string $direction: Either ADD or SUB.

int $count: The number of values to adjust.

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

File

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

Class

DateSqlHandler
A class to manipulate date SQL.

Namespace

Drupal\date_api

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}')";
      }
    case 'sqlite':
      $granularity .= 'S';
      switch ($direction) {
        case 'ADD':
          return "datetime({$field}, '+{$count} {$granularity}')";
        case 'SUB':
          return "datetime({$field}, '-{$count} {$granularity}')";
      }
  }
  return $field;
}