Get & Set Timezone In PHP (Very Simple Examples)

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 in php.ini, or use date_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!

ⓘ I have included a zip file with all the source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

 

 

TLDR – QUICK SLIDES

 

TABLE OF CONTENTS

 

DOWNLOAD & NOTES

Firstly, here is the download link to the example code as promised.

 

QUICK NOTES

If you spot a bug, feel free to comment below. I try to answer short questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

 

EXAMPLE CODE DOWNLOAD

Click here to download all the example source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

 

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

1-default-timezone.php
<?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:

  1. One has to first understand that the system’s default timezone is set with date.timezone in php.ini. This is usually UTC+0 on most international servers.
  2. To get the server’s default timezone in PHP scripts, simply use date_default_timezone_get().
  3. 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

2-date-time.php
<?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

3-unix.php
<?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.

 

 

EXTRA BITS & LINKS

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

 

INFOGRAPHIC CHEAT SHEET

Get & Set Timezone In PHP (click to enlarge)

 

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!

Leave a Comment

Your email address will not be published. Required fields are marked *