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, 20 May 2011

view this page in

DateTime::setTimezone

(PHP 5 >= 5.2.0)

DateTime::setTimezoneEstablece la zona horaria para el objeto DateTime

Descripción

Estilo orientado a objetos

public DateTime DateTime::setTimezone ( DateTimeZone $timezone )

Estilo por procesos

Parámetros

object

Estilo por procesos solamente: Un objeto DateTime devuelto por date_create(). La función modifica este objeto.

timezone

Un objeto DateTimeZone que representa la zona horaria deseada.

Valores devueltos

Devuelve el objecto DateTime por el método encadenado o FALSE en caso de error.

Historial de cambios

Versión Descripción
5.3.0Se ha cambiado el valor devuelto en caso de éxito de NULL a DateTime.

Ejemplos

Example #1 Ejemplo de DateTime::setTimeZone()

Estilo orientado a objetos

<?php
$fecha 
= new DateTime('2000-01-01', new DateTimeZone('Pacific/Nauru'));
echo 
$fecha->format('Y-m-d H:i:sP') . "\n";

$fecha->setTimezone(new DateTimeZone('Pacific/Chatham'));
echo 
$fecha->format('Y-m-d H:i:sP') . "\n";
?>

Estilo por procesos

<?php
$fecha
date_create('2000-01-01'timezone_open('Pacific/Nauru'));
echo 
date_format($fecha'Y-m-d H:i:sP') . "\n";

date_timezone_set($fechatimezone_open('Pacific/Chatham'));
echo 
date_format($fecha'Y-m-d H:i:sP') . "\n";
?>

El resultado de los ejemplos serían:

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

Ver también



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.

 
show source | credits | stats | sitemap | contact | advertising | mirror sites