You are here

public function DateObject::setTimezone in Date 7

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

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.

1 call to DateObject::setTimezone()
DateObject::__construct in date_api/date_api.module
Overridden constructor.

File

date_api/date_api.module, line 211
This module will make the date API available to other modules. Designed to provide a light but flexible assortment of functions and constants, with more functionality in additional files that are not loaded unless other modules specifically include them.

Class

DateObject
Extend PHP DateTime class with granularity handling, merge functionality and slightly more flexible initialization parameters.

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);
}