1.20.x date.class.inc | public BackdropDateTime::setTimezone($timezone, $force = FALSE) |
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
DateTimeZone $timezone: A timezone object.
bool $force: Whether or not to skip a date with no time. Defaults to FALSE.
Return value
DateTime: This object with the timezone updated.
File
- includes/
date.class.inc, line 200
Class
- BackdropDateTime
- Extends PHP DateTime class for use with Backdrop.
Code
public function setTimezone($timezone, $force = FALSE) {
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($timezone);
$this->setDate($arr['year'], $arr['month'], $arr['day']);
$this->setTime($arr['hour'], $arr['minute'], $arr['second']);
$this->addGranularity('timezone');
return $this;
}
return parent::setTimezone($timezone);
}