In Drupal 8, when creating a view with an exposed filter on a Date field (which is in date only mode), the resulting query removes some hours based on my timezone.

I'm in the UTC+2 timezone, so a date filter of 2016-04-20 creates a query of DATE_FORMAT('2016-04-19T22:00:00', '%Y-%m-%d'). And so, none of the nodes I want are displayed...

How to avoid that?

wrong query for an exposed date filter because of timezones


The following helps when rendering views programmatically:

$event_date_time = new \DateTime($query->get('date'), new \DateTimeZone('America/New_York'));
$event_date_time->modify('+1 day');
$event_date = $event_date_time->setTimezone(new \DateTimeZone('America/New_York'))->format('Ymd');

$view = \Drupal\views\Views::getView('events');
$output['events'] = $view->preview();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.