You are here

public function DateObject::setTimezone in Date 7.2

Same name and namespace in other branches
  1. 7.3 date_api/date_api.module \DateObject::setTimezone()
  2. 7 date_api/date_api.module \DateObject::setTimezone()

Sets the time zone for the current date.

Overrides default DateTime function. Only changes output values if actually had time granularity. This should be used as a "converter" for output, to switch tzs.

In order to set a timezone for a datetime that doesn't have such granularity, merge() it with one that does.

Parameters

object $tz: A timezone object.

bool $force: Whether or not to skip a date with no time. Defaults to FALSE.

1 call to DateObject::setTimezone()
DateObject::__construct in date_api/date_api.module
Constructs a date object.

File

date_api/date_api.module, line 370
This module will make the date API available to other modules.

Class

DateObject
Extend PHP DateTime class.

Code

public function setTimezone($tz, $force = FALSE) {

  // PHP 5.2.6 has a fatal error when setting a date's timezone to itself.
  // http://bugs.php.net/bug.php?id=45038
  if (version_compare(PHP_VERSION, '5.2.7', '<') && $tz == $this
    ->getTimezone()) {
    $tz = new DateTimeZone($tz
      ->getName());
  }
  if (!$this
    ->hasTime() || !$this
    ->hasGranularity('timezone') || $force) {

    // This has no time or timezone granularity, so timezone doesn't mean
    // much. We set the timezone using the method, which will change the
    // day/hour, but then we switch back.
    $arr = $this
      ->toArray(TRUE);
    parent::setTimezone($tz);
    $this
      ->setDate($arr['year'], $arr['month'], $arr['day']);
    $this
      ->setTime($arr['hour'], $arr['minute'], $arr['second']);
    $this
      ->addGranularity('timezone');
    return;
  }
  return parent::setTimezone($tz);
}