本文整理汇总了PHP中DateUtil::setTimeZone方法的典型用法代码示例。如果您正苦于以下问题:PHP DateUtil::setTimeZone方法的具体用法?PHP DateUtil::setTimeZone怎么用?PHP DateUtil::setTimeZone使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DateUtil
的用法示例。
在下文中一共展示了DateUtil::setTimeZone方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: diff
/** Diff two date objects. Only full units are returned */
public static function diff(TimeInterval $interval, Date $date1, Date $date2) : int
{
if ($date1->getOffsetInSeconds() != $date2->getOffsetInSeconds()) {
// Convert date2 to same timezone as date1. To work around PHP bug #45038,
// not just take the timezone of date1, but construct a new one which will
// have a timezone ID - which is required for this kind of computation.
$tz = new TimeZone(timezone_name_from_abbr('', $date1->getOffsetInSeconds(), $date1->toString('I')));
// Now, convert both dates to the same time (actually we only need to convert the
// second one, as the first will remain in the same timezone)
$date2 = $tz->translate($date2);
}
// Then cut off timezone, by setting both to GMT
$date1 = DateUtil::setTimeZone($date1, new TimeZone('GMT'));
$date2 = DateUtil::setTimeZone($date2, new TimeZone('GMT'));
switch ($interval) {
case TimeInterval::$YEAR:
return -($date1->getYear() - $date2->getYear());
case TimeInterval::$MONTH:
return -(($date1->getYear() - $date2->getYear()) * 12 + ($date1->getMonth() - $date2->getMonth()));
case TimeInterval::$DAY:
return -(intval($date1->getTime() / 86400) - intval($date2->getTime() / 86400));
case TimeInterval::$HOURS:
return -(intval($date1->getTime() / 3600) - intval($date2->getTime() / 3600));
case TimeInterval::$MINUTES:
return -(intval($date1->getTime() / 60) - intval($date2->getTime() / 60));
case TimeInterval::$SECONDS:
return -($date1->getTime() - $date2->getTime());
}
}
示例2: testSetTimezone
public function testSetTimezone()
{
$this->assertEquals(Date::create(2000, 1, 1, 17, 15, 11, new TimeZone('GMT')), DateUtil::setTimeZone($this->fixture, new TimeZone('America/New_York')));
}