PHP Timezones

Working with dates, times, and timezones in PHP

🌍 What are PHP Timezones?

Timezones help you handle dates and times across different geographical locations. PHP provides functions to set, convert, and display times in various timezones, ensuring accurate time handling for global applications.


<?php
// Set timezone and display time
date_default_timezone_set('America/New_York');
echo date('Y-m-d H:i:s');
?>
                                    

Output:

2024-03-15 14:30:45

Timezone Functions Overview

⚙️

Set Timezone

Configure default timezone

date_default_timezone_set() date_default_timezone_get()
🕐

DateTime Objects

Advanced date/time handling

DateTime DateTimeZone
🔄

Convert Times

Change between timezones

setTimezone() format()
📋

List Timezones

Get available timezones

timezone_identifiers_list() timezone_abbreviations_list()

🔹 Setting Default Timezone

Set the default timezone for all date and time functions in your script. This ensures consistent time handling throughout your application and prevents timezone-related errors.

<?php
// Set timezone to New York
date_default_timezone_set('America/New_York');
echo "New York: " . date('H:i:s') . "<br>";

// Change to London
date_default_timezone_set('Europe/London');
echo "London: " . date('H:i:s') . "<br>";

// Change to Tokyo
date_default_timezone_set('Asia/Tokyo');
echo "Tokyo: " . date('H:i:s') . "<br>";

// Get current timezone
echo "Current timezone: " . date_default_timezone_get();
?>

Output:

New York: 14:30:45
London: 19:30:45
Tokyo: 03:30:45
Current timezone: Asia/Tokyo

🔹 DateTime with Timezone

Create DateTime objects with specific timezones for more flexible date and time manipulation. This object-oriented approach provides better control over timezone conversions.

<?php
// Create DateTime with timezone
$date = new DateTime('now', new DateTimeZone('America/New_York'));
echo "New York: " . $date->format('Y-m-d H:i:s T') . "<br>";

// Create with specific date
$date2 = new DateTime('2024-12-25 10:00:00', new DateTimeZone('Europe/Paris'));
echo "Paris: " . $date2->format('Y-m-d H:i:s T') . "<br>";

// Current time in different timezone
$date3 = new DateTime('now', new DateTimeZone('Australia/Sydney'));
echo "Sydney: " . $date3->format('Y-m-d H:i:s T');
?>

Output:

New York: 2024-03-15 14:30:45 EDT
Paris: 2024-12-25 10:00:00 CET
Sydney: 2024-03-16 05:30:45 AEDT

🔹 Converting Between Timezones

Convert times from one timezone to another using the setTimezone() method. This is essential for displaying times to users in different geographical locations.

<?php
// Create time in New York
$date = new DateTime('2024-03-15 14:00:00', new DateTimeZone('America/New_York'));
echo "Original (NY): " . $date->format('Y-m-d H:i:s T') . "<br>";

// Convert to London
$date->setTimezone(new DateTimeZone('Europe/London'));
echo "London: " . $date->format('Y-m-d H:i:s T') . "<br>";

// Convert to Tokyo
$date->setTimezone(new DateTimeZone('Asia/Tokyo'));
echo "Tokyo: " . $date->format('Y-m-d H:i:s T') . "<br>";

// Convert to Los Angeles
$date->setTimezone(new DateTimeZone('America/Los_Angeles'));
echo "LA: " . $date->format('Y-m-d H:i:s T');
?>

Output:

Original (NY): 2024-03-15 14:00:00 EDT
London: 2024-03-15 18:00:00 GMT
Tokyo: 2024-03-16 03:00:00 JST
LA: 2024-03-15 11:00:00 PDT

🔹 Timezone Information

Get detailed information about timezones including offset from UTC and timezone abbreviations. This helps you understand timezone differences and display relevant information to users.

<?php
$timezone = new DateTimeZone('America/New_York');
$datetime = new DateTime('now', $timezone);

// Get timezone offset in seconds
$offset = $timezone->getOffset($datetime);
$hours = $offset / 3600;
echo "UTC Offset: " . $hours . " hours<br>";

// Get timezone name
echo "Timezone: " . $timezone->getName() . "<br>";

// Get location info
$location = $timezone->getLocation();
echo "Country: " . $location['country_code'] . "<br>";
echo "Latitude: " . $location['latitude'] . "<br>";
echo "Longitude: " . $location['longitude'];
?>

Output:

UTC Offset: -4 hours
Timezone: America/New_York
Country: US
Latitude: 40.71417
Longitude: -74.00639

🔹 Listing Available Timezones

Get a list of all available timezone identifiers in PHP. This is useful for creating timezone selection dropdowns or validating timezone input from users.

<?php
// Get all timezones
$timezones = timezone_identifiers_list();
echo "Total timezones: " . count($timezones) . "<br><br>";

// Show first 5 timezones
echo "Sample timezones:<br>";
for ($i = 0; $i < 5; $i++) {
    echo "- " . $timezones[$i] . "<br>";
}

// Get timezones for specific region
$us_timezones = timezone_identifiers_list(DateTimeZone::AMERICA);
echo "<br>US timezones: " . count($us_timezones);
?>

Output:

Total timezones: 426

Sample timezones:
- Africa/Abidjan
- Africa/Accra
- Africa/Addis_Ababa
- Africa/Algiers
- Africa/Asmara

US timezones: 168

🔹 Calculating Time Differences

Calculate the difference between times in different timezones. This is useful for scheduling, calculating durations, and displaying relative times to users worldwide.

<?php
// Create two times in different zones
$ny = new DateTime('2024-03-15 14:00:00', new DateTimeZone('America/New_York'));
$tokyo = new DateTime('2024-03-15 14:00:00', new DateTimeZone('Asia/Tokyo'));

// Calculate difference
$diff = $ny->diff($tokyo);
echo "Time difference: " . $diff->h . " hours<br>";

// Compare timestamps
$ny_time = $ny->getTimestamp();
$tokyo_time = $tokyo->getTimestamp();
$hours_diff = abs($ny_time - $tokyo_time) / 3600;
echo "Actual difference: " . $hours_diff . " hours";
?>

Output:

Time difference: 13 hours
Actual difference: 13 hours

🔹 Formatting Dates with Timezone

Display dates and times in various formats with timezone information. Proper formatting ensures users see times in a familiar and understandable format for their location.

<?php
$date = new DateTime('now', new DateTimeZone('America/New_York'));

// Different format options
echo "Full: " . $date->format('l, F j, Y g:i:s A T') . "<br>";
echo "Short: " . $date->format('m/d/Y h:i A') . "<br>";
echo "ISO 8601: " . $date->format('c') . "<br>";
echo "RFC 2822: " . $date->format('r') . "<br>";
echo "Timezone: " . $date->format('e (T)') . "<br>";
echo "UTC Offset: " . $date->format('P');
?>

Output:

Full: Friday, March 15, 2024 2:30:45 PM EDT
Short: 03/15/2024 02:30 PM
ISO 8601: 2024-03-15T14:30:45-04:00
RFC 2822: Fri, 15 Mar 2024 14:30:45 -0400
Timezone: America/New_York (EDT)
UTC Offset: -04:00

🧠 Test Your Knowledge

Which function sets the default timezone in PHP?