array ( 0 => 'index.php', 1 => 'PHP Manual', ), 'head' => array ( 0 => 'UTF-8', 1 => 'zh', ), 'this' => array ( 0 => 'datetime.settimezone.php', 1 => 'DateTime::setTimezone', ), 'up' => array ( 0 => 'class.datetime.php', 1 => 'DateTime', ), 'prev' => array ( 0 => 'datetime.settimestamp.php', 1 => 'DateTime::setTimestamp', ), 'next' => array ( 0 => 'datetime.sub.php', 1 => 'DateTime::sub', ), 'alternatives' => array ( ), 'source' => array ( 'lang' => 'zh', 'path' => 'reference/datetime/datetime/settimezone.xml', ), ); $setup["toc"] = $TOC; $setup["toc_deprecated"] = $TOC_DEPRECATED; $setup["parents"] = $PARENTS; manual_setup($setup); ?>
(PHP 5 >= 5.2.0, PHP 7, PHP 8)
DateTime::setTimezone -- date_timezone_set — 设置 DateTime 对象的时区
object
仅过程化风格:由 date_create() 返回的 DateTime 类型的对象。此函数会修改这个对象。
timezone
代表所需时区的 DateTimeZone 对象。
返回链式调用的 DateTime 对象。调用此方法时,底层的时间点不会更改。
示例 #1 DateTime::setTimeZone() 示例
面向对象风格
<?php
$date = new DateTime('2000-01-01', new DateTimeZone('Pacific/Nauru'));
echo $date->format('Y-m-d H:i:sP') . "\n";
$date->setTimezone(new DateTimeZone('Pacific/Chatham'));
echo $date->format('Y-m-d H:i:sP') . "\n";
?>
过程化风格
<?php
$date = date_create('2000-01-01', timezone_open('Pacific/Nauru'));
echo date_format($date, 'Y-m-d H:i:sP') . "\n";
date_timezone_set($date, timezone_open('Pacific/Chatham'));
echo date_format($date, 'Y-m-d H:i:sP') . "\n";
?>
以上示例会输出:
2000-01-01 00:00:00+12:00 2000-01-01 01:45:00+13:45