30

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 :-)

John
  • 303
  • 1
  • 3
  • 4

6 Answers6

45

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.

Treffynnon
  • 21,365
  • 6
  • 65
  • 98
  • 2
    This is also why PHP has like 7 different timezones for the state of Indiana, to account for all their crazy different DST rules: http://php.net/manual/en/timezones.php – Jimmy Sawczuk Apr 27 '11 at 16:28
  • All: I really appreciate everyone's help. Treffynnon: Thank you! I will give it a try. My code looks so primitive compared to yours, lol. Was I even close in my attempt? Thanks again! – John Apr 27 '11 at 16:35
  • @John glad you like it. I am not sure about your code as I have never written it that way with the procedural PHP style. – Treffynnon Apr 27 '11 at 16:39
  • @Treffynnon To quote star wars: it's an older code but checks out. – Mr_Chimp Sep 30 '14 at 12:36
6

I know this is an old post, but there is another line you need to add to get the correct time.

Before converting to local time, you need to set the default time zone to UTC like this (if it is the time zone of the time you are providing):

function GmtTimeToLocalTime($time) {
    date_default_timezone_set('UTC');
    $new_date = new DateTime($time);
    $new_date->setTimeZone(new DateTimeZone('America/New_York'));
    return $new_date->format("Y-m-d h:i:s");
}
lcompare
  • 917
  • 12
  • 10
0

I'll improve on Hasin Hayder's answer

date_default_timezone_set('America/New_York');  // Set timezone.
$utc_ts = strtotime("2011-04-27 02:45 UTC");  // UTC Unix timestamp.
echo date('Y-m-d H:i:s a T', $utc_ts);

It should output

2011-04-26 10:45:00 pm EDT

The difference is adding the source Timezone. strtotime() accepts timezone too you know! :p

0
date_default_timezone_set('America/New_York');  // Set timezone.
$utc_ts = strtotime("2011-04-27 02:45");  // UTC Unix timestamp.

Just after this executing this, $utc_ts contains the local time. PHP handles the DST itself.

=H=

Hasin Hayder
  • 818
  • 7
  • 8
  • 1
    This does not convert the time between time zones. It just creates a new unix timestamp for `2011-04-27 02:45`. The time zone in your example does nothing in this case and is inconsequential. – Treffynnon Apr 27 '11 at 15:56
  • 2
    There's no such a thing like UTC timestamp because timestamp is not affected by timezone. You can set any timezone in first line and $utc_ts will return same value. This code does nothing but sets timezone for future use in script, it doesn't affect timestamp itself. – Wh1T3h4Ck5 Apr 27 '11 at 17:06
0
<?php
    $time = new DateTime('now', new DateTimeZone(date_default_timezone_get()));
    $timeZone = $time->format('P');//Asia/Kolkata ... GET TimeZone From PHP ini Setting
    $tm_datetime = '08/08/2021 12:00 AM';
    $tm_tz_from = $timeZone;
    $tm_tz_to = 'UTC';
    $tm_format = 'Ymd\THis\Z';
    $dt = new DateTime($tm_datetime, new DateTimeZone($tm_tz_from));
    $dt->setTimeZone(new DateTimeZone($tm_tz_to));
    $utc_time_from =$dt->format("H:i:s");
    echo "UTC TIME".$utc_time_from;

?>
Amol
  • 55
  • 4
0

I dislike the answers that changes default timezone. The right way based in my experience living in a country with summer time change is to work always in UTC and then, when showing to user, change to localtime.

This is the one that I use, from one timezone to another (In this example from UTC to Europe/Madrid).

$newDateTime = new DateTime($sourcetime, new DateTimeZone("UTC")); 
$newDateTime->setTimezone(new DateTimeZone("Europe/Madrid")); 
echo $newDateTime->format("Y-m-d H:i:s")." ";

if you need the seconds for operation between dates then use $newDateTime->getTimestamp(), For example:

$difftime=time()-$newDateTime->getTimestamp();
Hamboy75
  • 938
  • 1
  • 11
  • 22