Home PHP Date/Time date_timezone_set() PHP date_timezone_set() Function Definition In PHP, date_timezone_set() changes the timezone of a DateTime object, so the same moment is shown as a clock time in another place.
It is the procedural name for DateTime::setTimezone() (and DateTimeImmutable::setTimezone()). The moment in time stays the same; only the way it is shown changes.
Syntax Call date_timezone_set() with the values you need. Start with this line.
date_timezone_set($date, timezone_open("Asia/Kolkata")); Call What it does date_timezone_set($date, timezone_open("Asia/Kolkata"))Shows the date in India time (UTC+5:30). date_timezone_set($date, timezone_open("America/New_York"))Shows the date in New York time. date_timezone_set($date, new DateTimeZone("UTC"))Shows the date in UTC.
This function converts the display time; it does not move the moment in time. The timestamp stays the same. For code that shares dates, use DateTimeImmutable::setTimezone(), which returns a new object and leaves the original alone.
Library declarations date_timezone_set(DateTime $object, DateTimeZone $timezone): DateTime Parameters Parameter Description object Required. The DateTime object to change. timezone Required. A DateTimeZone object, created with timezone_open() or new DateTimeZone().
Return Value Return Value Description DateTime The same DateTime object after the timezone has been changed.
PHP Examples 1. Convert a Time to Other Timezones 2. The Moment Stays the Same 3. Meeting Time in Several Cities Example 1 — Convert a Time to Other Timezones Start with 12:00 UTC and show the same moment in India and New York.
<?php
$date = date_create("2024-01-15 12:00:00", timezone_open("UTC"));
date_timezone_set($date, timezone_open("Asia/Kolkata"));
echo date_format($date, "Y-m-d H:i:s P"), PHP_EOL;
date_timezone_set($date, timezone_open("America/New_York"));
echo date_format($date, "Y-m-d H:i:s P"), PHP_EOL; 2024-01-15 17:30:00 +05:30
2024-01-15 07:00:00 -05:00 How It Works Create a date at 12:00 in UTC. Changing the timezone to Asia/Kolkata adds 5 hours 30 minutes, which gives 17:30. Changing it to America/New_York shows the same moment as 07:00, because New York is 5 hours behind UTC in winter. The format character P prints the offset for each timezone. Example 2 — The Moment Stays the Same Changing the timezone can change the date on the clock, but the timestamp is the same.
<?php
$date = date_create("2024-01-15 23:00:00", timezone_open("UTC"));
$before = date_timestamp_get($date);
date_timezone_set($date, timezone_open("Asia/Tokyo"));
echo date_format($date, "Y-m-d H:i T"), PHP_EOL;
echo $before === date_timestamp_get($date) ? "Same instant" : "Different instant", PHP_EOL; 2024-01-16 08:00 JST
Same instant How It Works The date starts at 23:00 on January 15 in UTC, and its timestamp is saved. Tokyo is 9 hours ahead of UTC, so the clock now shows 08:00 on January 16. The format character T prints the abbreviation JST. The timestamp is unchanged, so both times are the same moment. Frequently Asked Questions What does date_timezone_set() do? It changes the timezone of a DateTime object. The moment in time stays the same, but the clock time and date are shown for the new timezone.
Does date_timezone_set() change the timestamp? No. The timestamp always stays the same because the moment does not change. Only the displayed time changes.
What is the difference between date_timezone_set() and date_default_timezone_set()? date_timezone_set() changes one date object. date_default_timezone_set() changes the default timezone used by date functions for the whole script.
Does it change the original object? Yes, for DateTime it does. DateTimeImmutable::setTimezone() returns a new object, which is safer when you share dates.
Is date_timezone_set() the same as DateTime::setTimezone()? Yes. It is the procedural alias of setTimezone(), which also exists on DateTimeImmutable.
Next: getdate() getdate() turns a Unix timestamp into an array that holds the date and time parts, such as.
getdate() tutorial → About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
8 people found this page helpful
Helpful Share Copy link Suggestion