3 namespace Drupal\views\Plugin\views\query;
5 use Drupal\Core\Database\Connection;
8 * MySQL-specific date handling.
11 * This class should only be used by the Views SQL query plugin.
13 * @see \Drupal\views\Plugin\views\query\Sql
15 class MysqlDateSql implements DateSqlInterface {
18 * The database connection.
20 * @var \Drupal\Core\Database\Connection
25 * An array of PHP-to-MySQL replacement patterns.
27 protected static $replace = [
47 * Constructs the MySQL-specific date sql class.
49 * @param \Drupal\Core\Database\Connection $database
50 * The database connection.
52 public function __construct(Connection $database) {
53 $this->database = $database;
59 public function getDateField($field, $string_date) {
64 // Base date field storage is timestamp, so the date to be returned here is
65 // epoch + stored value (seconds from epoch).
66 return "DATE_ADD('19700101', INTERVAL $field SECOND)";
72 public function getDateFormat($field, $format) {
73 $format = strtr($format, static::$replace);
74 return "DATE_FORMAT($field, '$format')";
80 public function setTimezoneOffset($offset) {
81 $this->database->query("SET @@session.time_zone = '$offset'");
87 public function setFieldTimezoneOffset(&$field, $offset) {
88 if (!empty($offset)) {
89 $field = "($field + INTERVAL $offset SECOND)";