PHP: Convert UTC timestamp to local timestamp and vice versa

Updated: January 10, 2024 By: Guest Contributor Post a comment

Overview

Working with timezones in PHP is a common task for developers. This tutorial will guide you through converting UTC timestamps to local timestamps and the reverse operation, ensuring you handle time data accurately in your applications.

Understanding Timezones and UTC

Coordinated Universal Time (UTC) serves as the primary time standard by which the world regulates clocks and time. It does not observe Daylight Saving Time, remaining constant throughout the year. Converting between UTC and local time involves not just simple arithmetic but also an understanding of timezones and DST rules, which PHP can manage using its DateTime and DateTimeZone classes.

Basic Conversion from UTC to Local Time


$utcTimestamp = '2023-03-28 14:00:00';
$timezone = new DateTimeZone('America/New_York');
$utcDateTime = new DateTime($utcTimestamp, new DateTimeZone('UTC'));
$utcDateTime->setTimezone($timezone);

echo $utcDateTime->format('Y-m-d H:i:s');
// Output: 2023-03-28 10:00:00

Converting Local Timestamp to UTC


$localTimestamp = '2023-03-28 10:00:00';
$timezone = new DateTimeZone('America/New_York');
$localDateTime = new DateTime($localTimestamp, $timezone);
$localDateTime->setTimezone(new DateTimeZone('UTC'));

echo $localDateTime->format('Y-m-d H:i:s');
// Output: 2023-03-28 14:00:00

Working with Unix Timestamps

Unix timestamps represent seconds passed since the Unix Epoch (January 1 1970 00:00:00 GMT) and are timezone-less. PHP can also easily convert Unix timestamps to DateTime objects and switch timezones as needed.


$unixTimestamp = 1679995200; // corresponds to '2023-03-28 14:00:00' UTC
$timezone = new DateTimeZone('America/New_York');
$dateTime = (new DateTime())->setTimestamp($unixTimestamp);
$dateTime->setTimezone($timezone);

echo $dateTime->format('Y-m-d H:i:s');
// Output: 2023-03-28 10:00:00

Deeper into DateTime: Adjusting for Daylight Saving Time

PHP’s DateTime also automatically adjusts for Daylight Saving Time based on the timezone provided. This is critical when converting timestamps for dates when DST might apply.

Advanced Example with DST Handling


$timestamp = '2023-11-05 01:30:00'; // DST change date in America/New_York
$nyTimezone = new DateTimeZone('America/New_York');
$utcDateTime = new DateTime($timestamp, $nyTimezone);
$utcDateTime->setTimezone(new DateTimeZone('UTC'));

// During DST change, New York shifts from UTC-04:00 to UTC-05:00
// so 1:30 local time could be either before or after the change
$firstPossibleUTC = clone $utcDateTime;
$secondPossibleUTC = clone $utcDateTime;
$secondPossibleUTC->modify('+1 hour');

echo 'First possible UTC: ' . $firstPossibleUTC->format('Y-m-d H:i:s') . "\n";
echo 'Second possible UTC: ' . $secondPossibleUTC->format('Y-m-d H:i:s') . "\n";
// Potential Outputs:
// First possible UTC: 2023-11-05 05:30:00
// Second possible UTC: 2023-11-05 06:30:00

Handling Historical Dates and Timezones

PHP’s DateTime can consider historical timezone changes which are important for dates far in the past or future. Providers such as IANA regularly update timezones’ data which PHP uses to keep track of such historical adjustments.

Advanced Handling of Historical Timestamps


$historicTimestamp = '1918-03-31 02:00:00';
$nyTimezone = new DateTimeZone('America/New_York');
$historicDateTime = new DateTime($historicTimestamp, $nyTimezone);
$historicDateTime->setTimezone(new DateTimeZone('UTC'));
echo $historicDateTime->format('Y-m-d H:i:s');
// Output: 1918-03-31 07:00:00

Using Carbon for Enhanced Timezone Handling

Carbon is a popular PHP library that extends DateTime and simplifies date/time management. It automatically handles timezones and can make some of the previous examples simpler.

Example Using Carbon


use Carbon\Carbon;
use Carbon\CarbonTimeZone;

$carbonNow = Carbon::now(new CarbonTimeZone('UTC'));
echo 'UTC Now: ' . $carbonNow->toDateTimeString() . "\n";

$carbonNow->setTimezone('America/New_York');
echo 'New York Now: ' . $carbonNow->toDateTimeString();
// Outputs the current UTC time and then the equivalent time in New York.

Conclusion

In this tutorial, we’ve explored how to convert between UTC and local timestamps in PHP using several examples. Grasping timezone conversion is crucial for any application that operates across multiple time zones or presents time-related data to users geo-specifically. We covered built-in PHP functions and touched upon additional tools like Carbon that can further simplify your date and time programming in PHP.