Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert UTC dates to local time in PHP

I'm storing the UTC dates into the DB using:

$utc = gmdate("M d Y h:i:s A"); 

and then I want to convert the saved UTC date to the client's local time.

How can I do that?

Thanks

like image 254
Mark Avatar asked Sep 25 '10 01:09

Mark


People also ask

How do you convert UTC time to local time?

Add the local time offset to the UTC time. For example, if your local time offset is -5:00, and if the UTC time is shown as 11:00, add -5 to 11. The time setting when adjusted for offset is 06:00 (6:00 A.M.).

What is Strtotime PHP?

The strtotime() function parses an English textual datetime into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT). Note: If the year is specified in a two-digit format, values between 0-69 are mapped to 2000-2069 and values between 70-100 are mapped to 1970-2000.

How can we convert the time zones using PHP?

php $datetime = date("Y-m-d H:i:s"); $utc = new DateTime($datetime, new DateTimeZone('UTC')); $utc->setTimezone(new DateTimeZone('America/Sao_Paulo')); echo $utc->format('Y-m-d H:i:s'); ?>

What is UTC time now in 24 hour format?

UTC time in ISO-8601 is 00:48:11Z. Note that the Z letter without a space.


2 Answers

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.

like image 156
Timo Huovinen Avatar answered Sep 17 '22 17:09

Timo Huovinen


PHP's strtotime function will interpret timezone codes, like UTC. If you get the date from the database/client without the timezone code, but know it's UTC, then you can append it.

Assuming you get the date with timestamp code (like "Fri Mar 23 2012 22:23:03 GMT-0700 (PDT)", which is what Javascript code ""+(new Date()) gives):

$time = strtotime($dateWithTimeZone); $dateInLocal = date("Y-m-d H:i:s", $time); 

Or if you don't, which is likely from MySQL, then:

$time = strtotime($dateInUTC.' UTC'); $dateInLocal = date("Y-m-d H:i:s", $time); 
like image 22
webjprgm Avatar answered Sep 19 '22 17:09

webjprgm