Get & Set Timezone In PHP Date (Simple Examples)

Welcome to a tutorial on how to get and set the timezone in PHP. Working on an international project and have to deal with the timezones?

To get and set the timezone in PHP:

  • To get and set the system default timezone:
    • Change the date.timezone directive in the php.ini file, or use date_default_timezone_set(TIMEZONE) in the scripts.
    • Use date_default_timezone_get() to get the default system timezone.
  • To get and set the timezone on a DateTime Object:
    • $dt = new DateTime("2011-02-03", new DateTimeZone("Asia/Tokyo"));
    • echo $dt->getTimezone()->getName();
    • $dt->setTimezone("UTC");

That should cover the 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.

 

 

QUICK SLIDES

 

TABLE OF CONTENTS

Download & Notes PHP Timezone Useful Bits & Links
The End

 

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.

 

EXAMPLE 1) SYSTEM DEFAULT TIMEZONE

1-default-timezone.php
<?php
// (A) DEFAULT SYSTEM TIMEZONE 
// (A1) SET SYSTEM TIMEZONE WITH DATE.TIMEZONE DIRECTIVE IN PHP.INI
// https://www.php.net/manual/en/timezones.php
 
// (A2) GET CURRENT SYSTEM DEFAULT TIMEZONE
echo "System timezone : " . date_default_timezone_get() . "<br>";
 
// (A3) CHANGE SYSTEM TIMEZONE IN SCRIPTS
date_default_timezone_set("Antarctica/McMurdo");
echo "System timezone : " . date_default_timezone_get() . "<br>";
 
// (B) DATE TIME OBJECT WILL ASSUME DEFAULT SYSTEM TIMEZONE
// (B1) CURRENT DATE TIME
$now = new DateTime(); 
echo "Now : " . $now->format("d/M/Y H:i:s O") . "<br>";
 
// (B2) DEFINE A DATE TIME
$datetime = new DateTime("2019-08-07 06:05:04");
echo "Set : " . $datetime->format("d/M/Y H:i:s O") . "<br>";

To solve the mystery of timezones in PHP:

  • One has to first understand there is a default timezone set with date.timezone in php.ini. This is usually set to UTC on most international servers.
  • We can also change the system default timezone in the scripts using date_default_timezone_set().
  • To get or check the server’s default timezone in PHP, simply use date_default_timezone_get().

 

 

EXAMPLE 2) GET & SET TIMEZONE OF DATE-TIME OBJECTS

2-date-time.php
<?php
// (A) THIS WILL DEFAULT TO DEFAULT SYSTEM TIMEZONE
$dt = new DateTime("2019-08-07 06:05:04");
echo "Object Timezone : " . $dt->getTimezone()->getName() . "<br>";
echo $dt->format("d/M/Y H:i:s O") . "<br>";
 
// (B) SET TIMEZONE WHILE CREATING OBJECT
$dt = new DateTime("2019-08-07 06:05:04", new DateTimeZone("Asia/Tokyo"));
echo "Object Timezone : " . $dt->getTimezone()->getName() . "<br>";
echo $dt->format("d/M/Y H:i:s O") . "<br>";
 
// (C) READJUST TIMEZONE ON ABOVE DATE-TIME OBJECT
// NOTE : DATE TIME WILL ALSO BE ADJUSTED
$dt->setTimezone(new DateTimeZone("Antarctica/Troll"));
echo "Object Timezone : " . $dt->getTimezone()->getName() . "<br>";
echo $dt->format("d/M/Y H:i:s O") . "<br>";
  • When a new DateTime(DATE) object is created, it will default to the system timezone.
  • To specify a 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)).

 

 

EXAMPLE 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
// (B1) SIMULATE TIMEZONE UTC+8 
$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 default to UTC, and timezones are not well-supported; To “change” the timezone of a Unix Timestamp, we can only simulate by adding or subtracting a number of hours from the timestamp.

 

 

USEFUL 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.