You are here

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

Creates cross-database SQL date formatting.

Parameters

string $format: A format string for the result, like 'Y-m-d H:i:s'.

Return value

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

3 calls to HandlerBase::getSQLFormat()
CreatedFullDate::get_formula in lib/Views/node/Plugin/views/argument/CreatedFullDate.php
Overrides Drupal\views\Plugin\views\argument\Formula::get_formula().
CreatedYearMonth::get_formula in lib/Views/node/Plugin/views/argument/CreatedYearMonth.php
Overrides Drupal\views\Plugin\views\argument\Formula::get_formula().
Date::query in lib/Drupal/views/Plugin/views/sort/Date.php
Called to add the sort to a query.

File

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

Class

HandlerBase

Namespace

Drupal\views\Plugin\views

Code

public function getSQLFormat($format) {
  $db_type = Database::getConnection()
    ->databaseType();
  $field = $this
    ->getSQLDateField();
  switch ($db_type) {
    case 'mysql':
      $replace = array(
        'Y' => '%Y',
        'y' => '%y',
        'M' => '%b',
        'm' => '%m',
        'n' => '%c',
        'F' => '%M',
        'D' => '%a',
        'd' => '%d',
        'l' => '%W',
        'j' => '%e',
        'W' => '%v',
        'H' => '%H',
        'h' => '%h',
        'i' => '%i',
        's' => '%s',
        'A' => '%p',
      );
      $format = strtr($format, $replace);
      return "DATE_FORMAT({$field}, '{$format}')";
    case 'pgsql':
      $replace = array(
        'Y' => 'YYYY',
        'y' => 'YY',
        'M' => 'Mon',
        'm' => 'MM',
        'n' => 'MM',
        // no format for Numeric representation of a month, without leading zeros
        'F' => 'Month',
        'D' => 'Dy',
        'd' => 'DD',
        'l' => 'Day',
        'j' => 'DD',
        // no format for Day of the month without leading zeros
        'W' => 'WW',
        'H' => 'HH24',
        'h' => 'HH12',
        'i' => 'MI',
        's' => 'SS',
        'A' => 'AM',
      );
      $format = strtr($format, $replace);
      return "TO_CHAR({$field}, '{$format}')";
    case 'sqlite':
      $replace = array(
        'Y' => '%Y',
        // 4 digit year number
        'y' => '%Y',
        // no format for 2 digit year number
        'M' => '%m',
        // no format for 3 letter month name
        'm' => '%m',
        // month number with leading zeros
        'n' => '%m',
        // no format for month number without leading zeros
        'F' => '%m',
        // no format for full month name
        'D' => '%d',
        // no format for 3 letter day name
        'd' => '%d',
        // day of month number with leading zeros
        'l' => '%d',
        // no format for full day name
        'j' => '%d',
        // no format for day of month number without leading zeros
        'W' => '%W',
        // ISO week number
        'H' => '%H',
        // 24 hour hour with leading zeros
        'h' => '%H',
        // no format for 12 hour hour with leading zeros
        'i' => '%M',
        // minutes with leading zeros
        's' => '%S',
        // seconds with leading zeros
        'A' => '',
      );
      $format = strtr($format, $replace);
      return "strftime('{$format}', {$field}, 'unixepoch')";
  }
}