1 date_sql_handler.inc date_sql_handler::sql_where_date($type, $field, $operator, $value, $adjustment = NULL)

Creates a where clause to compare a complete date field to a date value.

Parameters

string $type: The type of value we're comparing to, could be another field or a date value.

string $field: The db table and field name, like "$table.$field".

string $operator: The db comparison operator to use, like '='.

int $value: The value to compare the extracted date part to, could be a field name or a date string or NOW().

Return value

string: SQL for the where clause for this operation.

File

core/modules/date/views/date_sql_handler.inc, line 329
SQL helper for Date API.

Class

date_sql_handler
A class to manipulate date SQL.

Code

function sql_where_date($type, $field, $operator, $value, $adjustment = NULL) {
  $type = strtoupper($type);
  if (strtoupper($value) == 'NOW') {
    $value = $this->sql_field('NOW', $adjustment);
  }
  elseif ($type == 'FIELD') {
    $value = $this->sql_field($value, $adjustment);
  }
  elseif ($type == 'DATE') {
    $date = new BackdropDateTime($value, date_default_timezone(), DATE_FORMAT_DATETIME);
    if (!empty($adjustment)) {
      date_modify($date, $adjustment . ' seconds');
    }
    // When comparing a field to a date we can avoid doing timezone
    // conversion by altering the comparison date to the db timezone.
    // This won't work if the timezone is a field instead of a value.
    if (empty($this->db_timezone_field) && empty($this->local_timezone_field) && $this->db_timezone_field != $this->local_timezone_field) {
      $date->setTimezone(timezone_open($this->db_timezone));
      $this->local_timezone = $this->db_timezone;
    }
    $value = "'" . $date->format(DATE_FORMAT_DATETIME, TRUE) . "'";
  }
  if ($this->local_timezone != $this->db_timezone) {
    $field = $this->sql_field($field);
  }
  else {
    $field = $this->sql_field($field, 0);
  }
  return "$field $operator $value";
}