DateTimePlus::prepareTimezone

protected DateTimePlus::prepareTimezone($timezone)

Prepares the input timezone value.

Changes the timezone before trying to use it, if necessary. Most importantly, makes sure there is a valid timezone object before moving further.

Parameters

mixed $timezone: Either a timezone name or a timezone object or NULL.

Return value

\DateTimeZone The massaged time zone.

File

core/lib/Drupal/Component/Datetime/DateTimePlus.php, line 390

Class

DateTimePlus
Wraps DateTime().

Namespace

Drupal\Component\Datetime

Code

protected function prepareTimezone($timezone) {
  // If the input timezone is a valid timezone object, use it.
  if ($timezone instanceof \DateTimezone) {
    $timezone_adjusted = $timezone;
  }

  // Allow string timezone input, and create a timezone from it.
  elseif (!empty($timezone) && is_string($timezone)) {
    $timezone_adjusted = new \DateTimeZone($timezone);
  }

  // Default to the system timezone when not explicitly provided.
  // If the system timezone is missing, use 'UTC'.
  if (empty($timezone_adjusted) || !$timezone_adjusted instanceof \DateTimezone) {
    $system_timezone = date_default_timezone_get();
    $timezone_name = !empty($system_timezone) ? $system_timezone : 'UTC';
    $timezone_adjusted = new \DateTimeZone($timezone_name);
  }

  // We are finally certain that we have a usable timezone.
  return $timezone_adjusted;
}
doc_Drupal
2016-10-29 09:01:03
Comments
Leave a Comment

Please login to continue.