©
이 문서에서는 PHP 중국어 웹사이트 매뉴얼 풀어 주다
(PHP 5 >= 5.2.0, PHP 7)
DateTime::setTimezone -- date_timezone_set — Sets the time zone for the DateTime object
面向对象风格
$timezone
)过程化风格
$object
, DateTimeZone $timezone
)Sets a new timezone for a DateTime object .
object
仅过程化风格:由 date_create() 返回的 DateTime 类型的对象。此函数会修改这个对象。
timezone
A DateTimeZone object representing the desired time zone.
返回被修改的 DateTime 对象, 或者在失败时返回 FALSE
.
版本 | 说明 |
---|---|
5.3.0 | 将返回值从 NULL 改为 DateTime 类型。 |
Example #1 DateTime::setTimeZone() example
面向对象风格
<?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
[#1] jamsilver at gmail dot com [2015-06-22 11:15:59]
In response to the other comments expressing surprise that changing the timezone does not affect the timestamp:
A UNIX timestamp is defined as the number of seconds that have elapsed since 00:00:00 (UTC), Thursday, 1 January 1970.
So: with respect to UTC. Always.
Calling setTimezone() never changes the actual "absolute", underlying, moment-in-time itself. It only changes the timezone you wish to "view" that moment "from". Consider the following:
<?php
// A time in London.
$datetime = new DateTime('2015-06-22T10:40:25', new DateTimeZone('Europe/London'));
// I wonder how that SAME moment-in-time would
// be described in other places around the world.
$datetime->setTimezone(new DateTimeZone('Australia/Sydney'));
print $datetime->format('Y-m-d H:i:s (e)');
// 2015-06-22 19:40:25 (Australia/Sydney)
$datetime->setTimezone(new DateTimeZone('America/New_York'));
print $datetime->format('Y-m-d H:i:s (e)');
// 2015-06-22 05:40:25 (America/New_York)
$datetime->setTimezone(new DateTimeZone('Asia/Calcutta'));
print $datetime->format('Y-m-d H:i:s (e)');
// 2015-06-22 15:10:25 (Asia/Calcutta)
?>
Please note that ALL of these date strings unambiguously represent the exact same moment-in-time. Therefore, calling getTimestamp() at any stage will return the same result:
<?php
$datetime->getTimestamp();
// 1434966025
?>
[#2] forzi at mail333 dot com [2014-09-30 15:02:46]
<?php
$MNTTZ = new DateTimeZone('America/Denver');
$ESTTZ = new DateTimeZone('America/New_York');
$dt = new DateTime('11/24/2009 2:00 pm', $MNTTZ);
var_dump($dt->format(DATE_RFC822), $dt->format('U'));
$dt->setTimezone($ESTTZ);
var_dump($dt->format(DATE_RFC822), $dt->format('U'));
?>
doesn't changes timestamp
But
<?php
$MNTTZ = new DateTimeZone('America/Denver');
$ESTTZ = new DateTimeZone('-0500');
$dt = new DateTime('11/24/2009 2:00 pm', $MNTTZ);
var_dump($dt->format(DATE_RFC822), $dt->format('U'));
$dt->setTimezone($ESTTZ);
var_dump($dt->format(DATE_RFC822), $dt->format('U'));
?>
changes timestamp
It is possible to fix that by:
<?php
$timestamp = $date->getTimestamp();
$date->setTimezone($value);
$date->setTimestamp($timestamp);
?>
But WTF???
[#3] salladin [2012-05-08 13:59:43]
I found unexpected behaviour when passing a timestamp.
timezone seems to always be GMT+0000 unless setTimezone() is set.
<?php
$MNTTZ = new DateTimeZone('America/Denver');
$ts = 1336476757;
$dt = new DateTime("@$ts", $MNTTZ);
var_dump($dt->format('T'), $dt->format('U'));
$dt->setTimezone($MNTTZ);
var_dump($dt->format('T'), $dt->format('U'));
?>
[#4] keithm at aoeex dot com [2009-11-24 15:13:17]
The timestamp value represented by the DateTime object is not modified when you set the timezone using this method. Only the timezone, and thus the resulting display formatting, is affected.
This can be seen using the following test code:
<?php
$MNTTZ = new DateTimeZone('America/Denver');
$ESTTZ = new DateTimeZone('America/New_York');
$dt = new DateTime('11/24/2009 2:00 pm', $MNTTZ);
var_dump($dt->format(DATE_RFC822), $dt->format('U'));
$dt->setTimezone($ESTTZ);
var_dump($dt->format(DATE_RFC822), $dt->format('U'));
?>
As such, you can use this to easily convert between timezones for display purposes.