You are here

function date_sql_handler::sql_where_date in Date 6

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. 7.3 date_api/date_api_sql.inc \date_sql_handler::sql_where_date()
  4. 7 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_sql.inc, line 361

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 = date_make_date($value, date_default_timezone_name(), DATE_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_timezone_set($date, timezone_open($this->db_timezone));
      $this->local_timezone = $this->db_timezone;
    }
    $value = "'" . date_format_date($date, 'custom', DATE_FORMAT_DATETIME) . "'";
  }
  if ($this->local_timezone != $this->db_timezone) {
    $field = $this
      ->sql_field($field);
  }
  else {
    $field = $this
      ->sql_field($field, 0);
  }
  return "{$field} {$operator} {$value}";
}