MysqlDateSql.php in Drupal 9
File
core/modules/views/src/Plugin/views/query/MysqlDateSql.php
View source
<?php
namespace Drupal\views\Plugin\views\query;
use Drupal\Core\Database\Connection;
class MysqlDateSql implements DateSqlInterface {
protected $database;
protected static $replace = [
'Y' => '%Y',
'y' => '%y',
'M' => '%b',
'm' => '%m',
'n' => '%c',
'F' => '%M',
'D' => '%a',
'd' => '%d',
'l' => '%W',
'j' => '%e',
'W' => '%v',
'H' => '%H',
'h' => '%h',
'i' => '%i',
's' => '%s',
'A' => '%p',
];
public function __construct(Connection $database) {
$this->database = $database;
}
public function getDateField($field, $string_date) {
if ($string_date) {
return $field;
}
return "DATE_ADD('19700101', INTERVAL {$field} SECOND)";
}
public function getDateFormat($field, $format) {
$format = strtr($format, static::$replace);
return "DATE_FORMAT({$field}, '{$format}')";
}
public function setTimezoneOffset($offset) {
$this->database
->query("SET @@session.time_zone = '{$offset}'");
}
public function setFieldTimezoneOffset(&$field, $offset) {
if (!empty($offset)) {
$field = "({$field} + INTERVAL {$offset} SECOND)";
}
}
}