Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP UTC to Local Time

Tags:

php

utc

dst

Server Environment

Redhat Enterprise Linux
PHP 5.3.5

Problem

Let's say I have a UTC date and time such as 2011-04-27 02:45 and I want to convert it to my local time, which is America/New_York.

Three questions:

1.) My code below might solve the problem, would you agree?

<?php  date_default_timezone_set('America/New_York');  // Set timezone.  $utc_ts = strtotime("2011-04-27 02:45");  // UTC Unix timestamp.  // Timezone offset in seconds. The offset for timezones west of UTC is always negative, // and for those east of UTC is always positive. $offset = date("Z");  $local_ts = $utc_ts + $offset;  // Local Unix timestamp. Add because $offset is negative.  $local_time = date("Y-m-d g:i A", $local_ts);  // Local time as yyyy-mm-dd h:m am/pm.  echo $local_time;  // 2011-04-26 10:45 PM  ?> 

2.) But, does the value of $offset automatically adjust for Daylight Savings Time (DST) ?
3.) If not, how should I tweak my code to automatically adjust for DST ?

Thank you :-)

like image 299
John Avatar asked Apr 27 '11 15:04

John


People also ask

How to convert UTC time to local timezone in PHP?

Answer. // 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');

How do you convert UTC to local?

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.). Note The date also follows UTC format.

How can I get UTC in PHP?

Use strtotime() to Get UTC Time In the following code, we get a timestamp from strtotime() using gmdate() . Then we pass the timestamp as the second parameter of the date() function.

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.


1 Answers

This will do what you want using PHPs native DateTime and DateTimeZone classes:

$utc_date = DateTime::createFromFormat(                 'Y-m-d G:i',                  '2011-04-27 02:45',                  new DateTimeZone('UTC') );  $nyc_date = $utc_date; $nyc_date->setTimeZone(new DateTimeZone('America/New_York'));  echo $nyc_date->format('Y-m-d g:i A'); // output: 2011-04-26 10:45 PM 

See DateTime::createFromFormat man page for more information.

After some experimentation between time zones that do and do not currently have DST I have discovered that this will take DST into account. The same conversion using my method above renders the same resulting time.

like image 81
Treffynnon Avatar answered Oct 04 '22 18:10

Treffynnon