1 utility.inc views_date_sql_field($field, $field_type = 'int', $set_offset = NULL)

Helper function to create cross-database SQL dates.


$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 Backdrop 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.:


core/modules/views/includes/utility.inc, line 341
Utility functions for assembling Views queries.


function views_date_sql_field($field, $field_type = 'int', $set_offset = NULL) {
  $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 ($field_type) {
    case 'int':
      $field = "DATE_ADD('19700101', INTERVAL $field SECOND)";
    case 'datetime':
  if (!empty($offset)) {
    $field = "($field + INTERVAL $offset_seconds SECOND)";
  return $field;