You are here

function date_sql_handler::sql_where_date in Date 7

Same name and namespace in other branches
  1. 5.2 date_api_sql.inc \date_sql_handler::sql_where_date()
  2. 6.2 date_api_sql.inc \date_sql_handler::sql_where_date()
  3. 6 date_api_sql.inc \date_sql_handler::sql_where_date()
  4. 7.3 date_api/date_api_sql.inc \date_sql_handler::sql_where_date()
  5. 7.2 date_api/date_api_sql.inc \date_sql_handler::sql_where_date()

Create a where clause to compare a complete date field to a complete 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

SQL for the where clause for this operation.

File

date_api/date_api_sql.inc, line 424
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 = 0) {
  $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 DateObject($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}";
}