function views_date_sql_field

Same name in other branches
  1. 6.x-3.x includes/handlers.inc \views_date_sql_field()

Helper function to create cross-database SQL dates.

Parameters

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

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

string $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

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

2 calls to views_date_sql_field()
views_date_sql_extract in includes/handlers.inc
Helper function to create cross-database SQL date extraction.
views_date_sql_format in includes/handlers.inc
Helper function to create cross-database SQL date formatting.

File

includes/handlers.inc, line 1298

Code

function views_date_sql_field($field, $field_type = 'int', $set_offset = NULL) {
    $db_type = Database::getConnection()->databaseType();
    $offset = $set_offset !== NULL ? $set_offset : views_get_timezone();
    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':
            switch ($field_type) {
                case 'int':
                    $field = "DATE_ADD('19700101', INTERVAL {$field} SECOND)";
                    break;
                case 'datetime':
                    break;
            }
            if (!empty($offset)) {
                $field = "({$field} + INTERVAL {$offset_seconds} SECOND)";
            }
            return $field;
        case 'pgsql':
            switch ($field_type) {
                case 'int':
                    $field = "TO_TIMESTAMP({$field})";
                    break;
                case 'datetime':
                    break;
            }
            if (!empty($offset)) {
                $field = "({$field} + INTERVAL '{$offset_seconds} SECONDS')";
            }
            return $field;
        case 'sqlite':
            if (!empty($offset)) {
                $field = "({$field} + '{$offset_seconds}')";
            }
            return $field;
    }
}