function DateTimeFormatterBase::setTimeZone

Same name in other branches
  1. 8.9.x core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeFormatterBase.php \Drupal\datetime\Plugin\Field\FieldFormatter\DateTimeFormatterBase::setTimeZone()
  2. 10 core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeFormatterBase.php \Drupal\datetime\Plugin\Field\FieldFormatter\DateTimeFormatterBase::setTimeZone()
  3. 11.x core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeFormatterBase.php \Drupal\datetime\Plugin\Field\FieldFormatter\DateTimeFormatterBase::setTimeZone()

Sets the proper time zone on a DrupalDateTime object for the current user.

A DrupalDateTime object loaded from the database will have the UTC time zone applied to it. This method will apply the time zone for the current user, based on system and user settings.

Parameters

\Drupal\Core\Datetime\DrupalDateTime $date: A DrupalDateTime object.

3 calls to DateTimeFormatterBase::setTimeZone()
DateTimeCustomFormatter::settingsSummary in core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeCustomFormatter.php
Returns a short summary for the current formatter settings.
DateTimeFormatterBase::buildDate in core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeFormatterBase.php
Creates a render array from a date object.
DateTimeFormatterBase::buildDateWithIsoAttribute in core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeFormatterBase.php
Creates a render array from a date object with ISO date attribute.

File

core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeFormatterBase.php, line 165

Class

DateTimeFormatterBase
Base class for 'DateTime Field formatter' plugin implementations.

Namespace

Drupal\datetime\Plugin\Field\FieldFormatter

Code

protected function setTimeZone(DrupalDateTime $date) {
    if ($this->getFieldSetting('datetime_type') === DateTimeItem::DATETIME_TYPE_DATE) {
        // A date without time has no timezone conversion.
        $timezone = DateTimeItemInterface::STORAGE_TIMEZONE;
    }
    else {
        $timezone = date_default_timezone_get();
    }
    $date->setTimeZone(timezone_open($timezone));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.