PHP Timezones

PHP Timezones – Set & Convert Timezones in PHP 🌍

Ever scheduled a meeting only to realize it was at 3 AM in someone else’s timezone? 🤦‍♂️
Timezones can be tricky, but PHP makes it easy to manage time across different locations!


🔹 Checking the Default Timezone

By default, PHP uses a system-defined timezone. You can check it using date_default_timezone_get().

<?php
echo "Default Timezone: " . date_default_timezone_get();
?>

Try It Now


🔹 Setting a Timezone

Use date_default_timezone_set() to change the timezone.

<?php
date_default_timezone_set("America/New_York");
echo "Timezone Set To: " . date_default_timezone_get();
?>

Try It Now

This sets the timezone to New York.


🔹 Getting the Current Time in a Specific Timezone

You can set a timezone and get the current time in that region.

<?php
date_default_timezone_set("Asia/Tokyo");
echo "Tokyo Time: " . date("Y-m-d H:i:s");
?>

Try It Now

This will display the current time in Tokyo, Japan.


🔹 List of Available Timezones

PHP provides a list of supported timezones.

<?php
$timezones = timezone_identifiers_list();
foreach ($timezones as $tz) {
    echo $tz . "<br>";
}
?>

Try It Now

You can pick any of these for your application.


🔹 Converting Between Timezones

Use DateTime and DateTimeZone to convert times between timezones.

<?php
$date = new DateTime("now", new DateTimeZone("UTC"));
$date->setTimezone(new DateTimeZone("America/Los_Angeles"));
echo "Time in Los Angeles: " . $date->format("Y-m-d H:i:s");
?>

Try It Now

This converts the current UTC time to Los Angeles time.


🔹 Handling Daylight Saving Time (DST)

Some locations change their clocks for DST. PHP automatically handles this when using timezones.

<?php
$date = new DateTime("2025-06-15", new DateTimeZone("America/New_York"));
echo "New York Time (DST Applied): " . $date->format("Y-m-d H:i:s");
?>

Try It Now

PHP adjusts the time automatically if the date falls within Daylight Saving Time (DST).


🎯 Key Takeaways

  • date_default_timezone_get() → Gets the current timezone.
  • date_default_timezone_set("Timezone") → Sets the timezone.
  • timezone_identifiers_list() → Lists all available timezones.
  • DateTime & DateTimeZone → Used for advanced timezone conversion.

📝 Practice Time!

Try setting different timezones and converting between them. Can you figure out the time in Sydney when it’s midnight in London? 🕰️