downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

DateTime::sub> <DateTime::setTimestamp
Last updated: Fri, 13 Nov 2009

view this page in

DateTime::setTimezone

(PHP 5 >= 5.2.0)

DateTime::setTimezoneDateTime オブジェクトのタイムゾーンを設定する

説明

public DateTime DateTime::setTimezone ( DateTimeZone $timezone )

パラメータ

object

手続き型のみ: date_create() が返す DateTime オブジェクト

timezone

指定したいタイムゾーン。

返り値

変更後の DateTime を返します。

変更履歴

バージョン 説明
5.3.0 返り値が NULL から DateTime に変更されました。

例1 DateTimeZone オブジェクトの設定と取得

<?php
date_default_timezone_set
('Europe/London');

$datetime = new DateTime('2008-08-03 12:35:23');
echo 
$datetime->getTimezone()->getName() . "\n";

$datetime = new DateTime('2008-08-03 12:35:23');
$la_time = new DateTimeZone('America/Los_Angeles');
$datetime->setTimezone($la_time);
echo 
$datetime->getTimezone()->getName();
?>

上の例の出力は以下となります。

Europe/London
America/Los_Angeles

参考



DateTime::sub> <DateTime::setTimestamp
Last updated: Fri, 13 Nov 2009
 
add a note add a note User Contributed Notes
DateTime::setTimezone
keithm at aoeex dot com
24-Nov-2009 11:13
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'));

/** Output:
string(29) "Tue, 24 Nov 09 14:00:00 -0700"
string(10) "1259096400"
string(29) "Tue, 24 Nov 09 16:00:00 -0500"
string(10) "1259096400"
**/
?>

As such, you can use this to easily convert between timezones for display purposes.
dan dot morin at gmail dot com
25-Jun-2009 07:56
In my setup setTimezone() does modify the date, as well as the TZ.

Code:

<?php
$dt_obj
= new DateTime('2009-06-25 12:00:00');
echo
$dt_obj->format(DATE_RFC822) . "\n";
$dt_obj->setTimezone(new DateTimeZone('GMT'));
echo
$dt_obj->format(DATE_RFC822);
?>

Output:
Thu, 25 Jun 09 12:00:00 -0400
Thu, 25 Jun 09 16:00:00 +0000
sht dot alien at gmx dot net
16-Jun-2009 04:13
Btw: setTimezone() does NOT modify the date, just the TZ. I tested this just to make sure 'cause the documentation doesn't tell.

DateTime::sub> <DateTime::setTimestamp
Last updated: Fri, 13 Nov 2009
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites