function date_sql_handler::sql_format in Date 5.2
Same name and namespace in other branches
- 6.2 date_api_sql.inc \date_sql_handler::sql_format()
- 6 date_api_sql.inc \date_sql_handler::sql_format()
- 7.3 date_api/date_api_sql.inc \date_sql_handler::sql_format()
- 7 date_api/date_api_sql.inc \date_sql_handler::sql_format()
- 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_sql.inc - Create a where clause to compare a formated field to a formated value.
File
- ./
date_api_sql.inc, line 311
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',
);
$format = strtr($format, $replace);
// Fix Views bug that does double replacement of %%d.
// TODO remove once the bug fix gets into an official Views release.
$format = str_replace('%%d', '***SQLD***', $format);
$format = str_replace('%%s', '***SQLS***', $format);
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}')";
}
}