You are here

function date_sql_handler::sql_format in Date 7

Same name and namespace in other branches
  1. 5.2 date_api_sql.inc \date_sql_handler::sql_format()
  2. 6.2 date_api_sql.inc \date_sql_handler::sql_format()
  3. 6 date_api_sql.inc \date_sql_handler::sql_format()
  4. 7.3 date_api/date_api_sql.inc \date_sql_handler::sql_format()
  5. 7.2 date_api/date_api_sql.inc \date_sql_handler::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' .

Return value

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

1 call to date_sql_handler::sql_format()
date_sql_handler::sql_where_format in date_api/date_api_sql.inc
Create a where clause to compare a formated field to a formated value.

File

date_api/date_api_sql.inc, line 319
SQL helper for Date API.

Class

date_sql_handler
A class to manipulate date SQL.

Code

function sql_format($format, $field) {
  switch ($this->db_type) {
    case 'mysql':
    case 'mysqli':
      $replace = array(
        'Y' => '%Y',
        'y' => '%y',
        'm' => '%m',
        'n' => '%c',
        'd' => '%d',
        'j' => '%e',
        'H' => '%H',
        'i' => '%i',
        's' => '%s',
        '\\WW' => 'W%U',
      );
      $format = strtr($format, $replace);
      return "DATE_FORMAT({$field}, '{$format}')";
    case 'pgsql':
      $replace = array(
        'Y' => 'YYYY',
        'y' => 'Y',
        'm' => 'MM',
        'n' => 'M',
        'd' => 'DD',
        'j' => 'D',
        'H' => 'HH24',
        'i' => 'MI',
        's' => 'SS',
        '\\T' => '"T"',
      );
      $format = strtr($format, $replace);
      return "TO_CHAR({$field}, '{$format}')";
  }
}