codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🐘

PHP

29 / 56 topics
27Date and Time in PHP28Date Functions29Timezones in PHP
Tutorials/PHP/Timezones in PHP
🐘PHP

Timezones in PHP

Updated 2026-05-15
10 min read

Timezones in PHP

Introduction

In PHP, handling date and time is a common task, especially when dealing with applications that operate across multiple regions. One crucial aspect of date and time management is working with different timezones. PHP provides robust functions to handle timezones, allowing developers to easily convert times between various regions and manage the display of dates according to local settings.

Concept

PHP uses the DateTime class along with the DateTimeZone class to work with timezones. The DateTime class represents a date and time, while the DateTimeZone class represents a timezone. By combining these two classes, you can create date and time objects that are aware of their respective timezones.

Setting the Default Timezone

Before working with specific timezones, it's important to set the default timezone for your PHP application. This is done using the date_default_timezone_set function. The default timezone affects all date and time functions unless explicitly specified otherwise.

<?php
// Set the default timezone to Eastern Standard Time (EST)
date_default_timezone_set('America/New_York');
?>

Creating a DateTime Object with a Specific Timezone

You can create a DateTime object with a specific timezone by passing a DateTimeZone object as an argument to the constructor.

<?php
// Create a DateTime object for the current date and time in UTC
$utcTime = new DateTime('now', new DateTimeZone('UTC'));

// Output the current UTC time
echo $utcTime->format('Y-m-d H:i:s');
?>

Converting Between Timezones

To convert a DateTime object from one timezone to another, you can use the setTimezone method.

<?php
// Create a DateTime object for the current date and time in UTC
$utcTime = new DateTime('now', new DateTimeZone('UTC'));

// Convert the UTC time to Eastern Standard Time (EST)
$estTime = clone $utcTime;
$estTime->setTimezone(new DateTimeZone('America/New_York'));

// Output both times
echo "UTC Time: " . $utcTime->format('Y-m-d H:i:s') . "\n";
echo "EST Time: " . $estTime->format('Y-m-d H:i:s') . "\n";
?>

Listing Available Timezones

PHP provides a list of supported timezones that you can use with the DateTimeZone class. You can retrieve this list using the DateTimeZone::listIdentifiers method.

<?php
// List all available timezones
$timezones = DateTimeZone::listIdentifiers();

// Output the first 10 timezones
foreach (array_slice($timezones, 0, 10) as $timezone) {
    echo $timezone . "\n";
}
?>

Examples

Example 1: Displaying Current Time in Different Timezones

<?php
// Define an array of timezones to display the current time for
$timezones = [
    'UTC',
    'America/New_York',
    'Europe/London',
    'Asia/Tokyo'
];

foreach ($timezones as $timezone) {
    // Create a DateTime object for the current date and time in the specified timezone
    $time = new DateTime('now', new DateTimeZone($timezone));

    // Output the current time in the specified timezone
    echo "Current time in $timezone: " . $time->format('Y-m-d H:i:s') . "\n";
}
?>

Example 2: Converting a Specific Date and Time Between Timezones

<?php
// Define a specific date and time
$specificTime = '2023-10-01 15:00:00';

// Create a DateTime object for the specified date and time in UTC
$utcTime = new DateTime($specificTime, new DateTimeZone('UTC'));

// Convert the UTC time to Eastern Standard Time (EST)
$estTime = clone $utcTime;
$estTime->setTimezone(new DateTimeZone('America/New_York'));

// Output both times
echo "Specific UTC Time: " . $utcTime->format('Y-m-d H:i:s') . "\n";
echo "Specific EST Time: " . $estTime->format('Y-m-d H:i:s') . "\n";
?>

What's Next?

In the next section, we will explore file handling in PHP. You'll learn how to read from and write to files, manage directories, and handle file uploads. This knowledge is essential for building robust applications that interact with the filesystem.

Stay tuned!


PreviousDate FunctionsNext File Handling in PHP

Recommended Gear

Date FunctionsFile Handling in PHP