You are here

function views_date_sql_format in Views (for Drupal 7) 6.2

Same name and namespace in other branches
  1. 6.3 includes/handlers.inc \views_date_sql_format()
  2. 7.3 includes/handlers.inc \views_date_sql_format()

Helper function to create cross-database SQL date formatting.

Parameters

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

$field: The real table and field name, like 'tablename.fieldname'.

$field_type: The type of date field, 'int' or 'datetime'.

$set_offset: The name of a field that holds the timezone offset or a fixed timezone offset value. If not provided, the normal Drupal timezone handling will be used, i.e. $set_offset = 0 will make no timezone adjustment.

Return value

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

3 calls to views_date_sql_format()
views_handler_argument_node_created_fulldate::construct in modules/node/views_handler_argument_dates_various.inc
Constructor implementation
views_handler_argument_node_created_year_month::construct in modules/node/views_handler_argument_dates_various.inc
Constructor implementation
views_handler_sort_date::query in handlers/views_handler_sort_date.inc
Called to add the sort to a query.

File

includes/handlers.inc, line 928
handlers.inc Defines the various handler objects to help build and display views.

Code

function views_date_sql_format($format, $field, $field_type = 'int', $set_offset = NULL) {
  $db_type = $GLOBALS['db_type'];
  $field = views_date_sql_field($field, $field_type, $set_offset);
  switch ($db_type) {
    case 'mysql':
    case 'mysqli':
      $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}')";
  }
}