You are here

function date_sql_handler::sql_offset in Date 5.2

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

Adjust a field value by an offset in seconds.

2 calls to date_sql_handler::sql_offset()
date_sql_handler::sql_field in ./date_api_sql.inc
Helper function to create cross-database SQL dates.
date_sql_handler::sql_tz in ./date_api_sql.inc
Select a date value from the database, adjusting the value for the timezone.

File

./date_api_sql.inc, line 194

Class

date_sql_handler
A class to manipulate date SQL.

Code

function sql_offset($field, $offset = NULL) {
  if (!empty($offset)) {
    $op = $offset < 0 ? '-' : '';
    switch ($this->db_type) {
      case 'mysql':
      case 'mysqli':
        if (version_compare(db_version(), '4.1.1', '>=')) {
          return "ADDTIME({$field}, SEC_TO_TIME({$offset}))";
        }
        else {
          return "DATE_ADD({$field}, INTERVAL {$offset} SECOND)";
        }
      case 'pgsql':
        return "({$field} + INTERVAL '{$offset} SECONDS')";
    }
  }
  return $field;
}