Welcome to a tutorial on how to get and set the timezone in PHP. So you are working on an international project, and have to deal with timezones?
- Use
date_default_timezone_get()
to get the default system timezone. - To set the default system timezone, change the
date.timezone
directive inphp.ini
, or usedate_default_timezone_set(TIMEZONE)
in the scripts. - To get the timezone of a
DateTime
Object:$dt = new DateTime();
echo $dt->getTimezone()->getName();
- To set or change the timezone of a
DateTime
Object:$dt = new DateTime("YYYY-MM-DD", new DateTimeZone("Asia/Tokyo"));
$dt->setTimezone("UTC");
That covers the quick basics, but let us walk through more examples in this guide – Read on!
TABLE OF CONTENTS
DOWNLOAD & NOTES
Here is the download link to the example code, so you don’t have to copy-paste everything.
EXAMPLE CODE DOWNLOAD
Just click on “download zip” or do a git clone. I have released it under the MIT license, so feel free to build on top of it or use it in your own project.
SORRY FOR THE ADS...
But someone has to pay the bills, and sponsors are paying for it. I insist on not turning Code Boxx into a "paid scripts" business, and I don't "block people with Adblock". Every little bit of support helps.
Buy Me A Coffee Code Boxx eBooks
PHP TIMEZONE
All right, let us now get into the examples of how to get and set the timezone in PHP.
PART 1) SYSTEM DEFAULT TIMEZONE
<?php
// (A) SET SYSTEM TIMEZONE WITH DATE.TIMEZONE DIRECTIVE IN PHP.INI
// https://www.php.net/manual/en/timezones.php
// (B) GET SYSTEM DEFAULT TIMEZONE
$tz = date_default_timezone_get();
echo "System timezone: $tz<br>";
// (C) CHANGE SYSTEM TIMEZONE
date_default_timezone_set("Antarctica/McMurdo");
$tz = date_default_timezone_get();
echo "System timezone: $tz<br>";
To solve the mystery of timezones in PHP:
- One has to first understand that the system’s default timezone is set with
date.timezone
inphp.ini
. This is usually UTC+0 on most international servers. - To get the server’s default timezone in PHP scripts, simply use
date_default_timezone_get()
. - We can change the system default timezone in the scripts using
date_default_timezone_set()
.
P.S. For the full list of supported timezones, see the links in the extras section below.
PART 2) TIMEZONE OF DATE-TIME OBJECTS
<?php
// (A) DATE-TIME OBJECT WITH DEFAULT SYSTEM TIMEZONE
$dt = new DateTime("2019-08-07 06:05:04");
$tz = $dt->getTimezone()->getName();
echo "Object Timezone : $tz<br>";
echo $dt->format("d/M/Y H:i:s O") . "<br>";
// (B) SET TIMEZONE WHILE CREATING DATE-TIME OBJECT
$dt = new DateTime("2019-08-07 06:05:04", new DateTimeZone("Asia/Tokyo"));
$tz = $dt->getTimezone()->getName();
echo "Object Timezone : $tz<br>";
echo $dt->format("d/M/Y H:i:s O") . "<br>";
// (C) CHANGE TIMEZONE OF DATE-TIME OBJECT
// NOTE : DATE TIME WILL ALSO BE ADJUSTED
$dt->setTimezone(new DateTimeZone("Antarctica/Troll"));
$tz = $dt->getTimezone()->getName();
echo "Object Timezone : $tz<br>";
echo $dt->format("d/M/Y H:i:s O") . "<br>";
- When a
new DateTime()
object is created, it will default to the system timezone. - To specify a different timezone, use
new DateTime(DATE, new DateTimeZone(TIMEZONE))
instead. - To check the timezone of a
DateTime
object, use$DATETIME->getTimeZone()->getName()
. - Lastly, to adjust the timezone, use
$DATETIME->setTimezone(new DateTimeZone(TIMEZONE))
.
PART 3) TIMEZONES WITH UNIX TIMESTAMPS
<?php
// (A) UNIX TIMESTAMP - DEFAULTS TO UTC
$unix = strtotime("2011-02-03 04:05:06");
echo date("d/M/Y H:i:s O", $unix) . "<br>";
// (B) UNIX TIMESTAMP DOES NOT SUPPORT TIMEZONE
// MANUAL ADD SECONDS TO SIMULATE TIMEZONE
$utc8 = $unix + 28800; // 8 hrs = 28800 secs
echo date("d/M/Y H:i:s", $utc8) . "<br>";
As a small extra for you guys who are working with Unix Timestamps – They are simply the number of seconds that have elapsed since 1 Jan 1970 UTC+0. To “change the timezone” of a Unix Timestamp, we can only simulate by adding or subtracting a number of seconds from the timestamp.
EXTRAS
That’s all for the main tutorial, and here is a small section on some extras and links that may be useful to you.
LINKS & REFERENCES
- List of Supported Timezones – PHP
- Get System Default Timezone – PHP
- Set System Default Timezone – PHP
- Get Timezone – PHP
- Set Timezone – PHP
- Date Time Zone Class – PHP
THE END
Thank you for reading, and we have come to the end. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!