You are here

public function HandlerBase::getSQLDateField in Views (for Drupal 7) 8.3

Creates cross-database SQL dates.

Return value

string An appropriate SQL string for the db type and field type.

2 calls to HandlerBase::getSQLDateField()
Date::extractSQL in lib/Drupal/views/Plugin/views/argument/Date.php
Creates cross-database SQL date extraction.
HandlerBase::getSQLFormat in lib/Drupal/views/Plugin/views/HandlerBase.php
Creates cross-database SQL date formatting.

File

lib/Drupal/views/Plugin/views/HandlerBase.php, line 710
Definition of Drupal\views\Plugin\views\HandlerBase.

Class

HandlerBase

Namespace

Drupal\views\Plugin\views

Code

public function getSQLDateField() {
  $field = "{$this->tableAlias}.{$this->realField}";
  $db_type = Database::getConnection()
    ->databaseType();
  $offset = $this
    ->getTimezone();
  if (isset($offset) && !is_numeric($offset)) {
    $dtz = new \DateTimeZone($offset);
    $dt = new \DateTime('now', $dtz);
    $offset_seconds = $dtz
      ->getOffset($dt);
  }
  switch ($db_type) {
    case 'mysql':
      $field = "DATE_ADD('19700101', INTERVAL {$field} SECOND)";
      if (!empty($offset)) {
        $field = "({$field} + INTERVAL {$offset_seconds} SECOND)";
      }
      return $field;
    case 'pgsql':
      $field = "TO_TIMESTAMP({$field})";
      if (!empty($offset)) {
        $field = "({$field} + INTERVAL '{$offset_seconds} SECONDS')";
      }
      return $field;
    case 'sqlite':
      if (!empty($offset)) {
        $field = "({$field} + '{$offset_seconds}')";
      }
      return $field;
  }
}