Convert UTC dates to local time in PHP

If by client, you mean browser, then you first need to send the timezone name to PHP from the browser, then do the conversion as described below.

Answer

Convert the UTC datetime to America/Denver

// create a $dt object with the UTC timezone
$dt = new DateTime('2016-12-12 12:12:12', new DateTimeZone('UTC'));

// change the timezone of the object without changing its time
$dt->setTimezone(new DateTimeZone('America/Denver'));

// format the datetime
$dt->format('Y-m-d H:i:s T');

This works with dates after 2032, daylight savings and leap seconds and does not depend on the host machine locale or timezone.

It uses the timezonedb to do the calculation, this db changes over time as timezone rules change and has to be kept up to date. (see notes at the bottom)

To convert the UTC date to the server (local) time, you can use DateTime without the second argument, which defaults to the server timezone.

// create a $dt object with the UTC timezone
$dt = new DateTime('2016-12-12 12:12:12', new DateTimeZone('UTC'));

// get the local timezone
$loc = (new DateTime)->getTimezone();

// change the timezone of the object without changing its time
$dt->setTimezone($loc);

// format the datetime
$dt->format('Y-m-d H:i:s T');

Answer 2

I recommend using DateTimeImmutable because it doesn’t mutate variables (doesn’t change them behind the scenes), otherwise it works just like DateTime.

// create a $dt object with the UTC timezone
$dt_utc = new DateTimeImmutable('2016-12-12 12:12:12', new DateTimeZone('UTC'));

// Create a new instance with the new timezone
$dt_denver = $dt_utc->setTimezone(new DateTimeZone('America/Denver'));

// format the datetime
$dt_denver->format('Y-m-d H:i:s T');

Immutability allows you to use chaining multiple times without changing the value of $dt

$dt = new DateTimeImmutable('2016-12-12 12:12:12', new DateTimeZone('UTC'));

// Format $dt in Denver timezone
echo $dt->setTimezone(new DateTimeZone('America/Denver'))->format('Y-m-d H:i:s T');

// Format $dt in Madrid timezone
echo $dt->setTimezone(new DateTimeZone('Europe/Madrid'))->format('Y-m-d H:i:s T');

// Format $dt in Local server timezone
echo $dt->setTimezone((new DateTime())->getTimezone())->format('Y-m-d H:i:s T');

Notes

time() returns the unix timestamp, which is a number, it has no timezone.

date('Y-m-d H:i:s T') returns the date in the current locale timezone.

gmdate('Y-m-d H:i:s T') returns the date in UTC

date_default_timezone_set() changes the current locale timezone

to change a time in a timezone

// create a $dt object with the America/Denver timezone
$dt = new DateTime('2016-12-12 12:12:12', new DateTimeZone('America/Denver'));

// change the timezone of the object without changing it's time
$dt->setTimezone(new DateTimeZone('UTC'));

// format the datetime
$dt->format('Y-m-d H:i:s T');

here you can see all the available timezones

https://en.wikipedia.org/wiki/List_of_tz_database_time_zones

here are all the formatting options

http://php.net/manual/en/function.date.php

Update PHP timezone DB (in linux)

sudo pecl install timezonedb

Because of daylight savings, some dates repeat in some timezones, for example, in the United States, March 13, 2011 2:15am never occurred, while November 6, 2011 1:15am occurred twice. These datetimes can’t be accurately determined.

Leave a Comment