0

When I use an offset like "-4 hours" in a Views filter on a datetime field, which timezone is used?

More details

In Views, when I add a filter for a datetime field, I get the following options for Value type:

  • A date in any machine readable format. CCYY-MM-DD HH:MM:SS is preferred.
  • An offset from the current time such as "+1 day" or "-2 hours -30 minutes"

On my site, users can set their own timezones. (/admin/config/regional/settings Users may set their own time zone)

So there are potentially three timezones in play:

  • In the database, the strings for datetime are stored in the UTC timezone
  • The site timezone is set to Tokyo
  • The user timezone is set to Berlin

So, which of these timezones does the offset use?

2

For datetime fields, it uses the timezone for the user viewing the page. If no user timezone is set, it uses the default site timezone.

See the relevant comment from Drupal\datetime\Plugin\views\filter\Date.php:

Date-only fields do not have a time zone associated with them, so the filter input needs to use UTC for reference. Otherwise, use the time zone for the current user.

/**
 * Get the proper time zone to use in computations.
 *
 * Date-only fields do not have a time zone associated with them, so the
 * filter input needs to use UTC for reference. Otherwise, use the time zone
 * for the current user.
 *
 * @return string
 *   The time zone name.
 */
protected function getTimezone() {
  return $this->dateFormat === DateTimeItemInterface::DATE_STORAGE_FORMAT
    ? DateTimeItemInterface::STORAGE_TIMEZONE
    : date_default_timezone_get();
}
| improve this answer | |

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.