PHP Unconference Europe 2015

DateTime::setTimezone

date_timezone_set

(PHP 5 >= 5.2.0)

DateTime::setTimezone -- date_timezone_setSets the time zone for the DateTime object

Beschreibung

Objektorientierter Stil

public DateTime DateTime::setTimezone ( DateTimeZone $timezone )

Prozeduraler Stil

Sets a new timezone for a DateTime object.

Parameter-Liste

object

Nur bei prozeduralem Aufruf: Ein von date_create() zurückgegebenes DateTime-Objekt. Diese Funktion verändert dieses Objekt.

timezone

A DateTimeZone object representing the desired time zone.

Rückgabewerte

Gibt das DateTime-Objekt für die Verkettung von Methoden zurück Im Fehlerfall wird FALSE zurückgegeben.

Changelog

Version Beschreibung
5.3.0Der Rückgabewert bei Erfolg wurde von NULL auf DateTime geändert.

Beispiele

Beispiel #1 DateTime::setTimeZone() example

Objektorientierter Stil

<?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";
?>

Prozeduraler Stil

<?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($datetimezone_open('Pacific/Chatham'));
echo 
date_format($date'Y-m-d H:i:sP') . "\n";
?>

Die obigen Bespiele erzeugen folgende Ausgabe:

2000-01-01 00:00:00+12:00
2000-01-01 01:45:00+13:45

Siehe auch

add a note add a note

User Contributed Notes 3 notes

up
4
salladin
2 years ago
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'));

/** Output:
string(8) "GMT+0000"
string(10) "1336476757"
string(3) "MDT"
string(10) "1336476757"
**/

?>
up
4
keithm at aoeex dot com
4 years ago
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.
up
1
forzi at mail333 dot com
1 month ago
<?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???
To Top