DateTime::setTimezone

DateTime::setTimezone

date_timezone_set

(PHP 5 >= 5.2.0, PHP 7)

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

Description

Object oriented style

public DateTime DateTime::setTimezone ( DateTimeZone $timezone )

Procedural style

DateTime date_timezone_set ( DateTime $object , DateTimeZone $timezone )

Sets a new timezone for a DateTime object.

Parameters

object

Procedural style only: A DateTime object returned by date_create(). The function modifies this object.

timezone

A DateTimeZone object representing the desired time zone.

Return Values

Returns the DateTime object for method chaining or FALSE on failure.

Changelog

Version Description
5.3.0 Changed the return value on success from NULL to DateTime.

Examples

Example #1 DateTime::setTimeZone() example

Object oriented style

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

Procedural style

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

The above examples will output:

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

See Also

© 1997–2017 The PHP Documentation Group
Licensed under the Creative Commons Attribution License v3.0 or later.
https://secure.php.net/manual/en/datetime.settimezone.php

在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号

意见反馈
返回顶部