本文整理匯總了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')));
}