93

I am trying to write a script where i want to convert any timezone to UTC and reverse. But from some where i came to know that while converting any timezone to UTC with or without DST consideration it will give the same UTC time. For example: If i try to convert this one:

$mytime = '2011-03-31 05:06:00.000';
$myzone = 'America/New_York';

to UTC with DST and without DST,i will get ..

(New_York->UTC DST=Yes)2011-03-31 09:06:00
(New_York->UTC DST=No)2011-03-31 09:06:00 ..........

Is this corect ??If yes then why??? Please anybody give me your answers.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
0001
  • 943
  • 1
  • 6
  • 7

2 Answers2

157

No, UTC itself never has DST. It is the constant frame of reference other time zones are expressed relative to.

From the Wikipedia UTC page:

UTC does not change with a change of seasons, but local time or civil time may change if a time zone jurisdiction observes daylight saving time or summer time. For example, UTC is 5 hours ahead of local time on the east coast of the United States during winter, but 4 hours ahead during summer.

In other words, when a time zone observes DST, its offset from UTC changes when there's a DST transition, but that's that time zone observing DST, not UTC.

Without knowing much about PHP time zone handling, it seems strange to me that you can specify "with DST" or "without DST" in a conversion - the time zones themselves specify when DST kicks in... it shouldn't have to be something you specify yourself.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 2
    "Without knowing much about PHP time zone handling, it seems strange to me that you can specify 'with DST' or 'without DST' in a conversion"...that's probably because you haven't done much Programmable Hyperlinked Pasta. This is the language with a builtin `date_sunrise` function to tell you when the sun rises. I'm not sure if DST is needed for conversion, but it is a first-class citizen of a date object: http://php.net/manual/en/function.date.php. – Paul Draper Jul 16 '14 at 18:45
  • @PaulDraper: Being a first-class citizen of the date object makes a certain amount of sense - just not specifying it separately for the conversion. I've since learned a bit more about some of the underlying C routines, however, and I suspect they have a lot to do with this... – Jon Skeet Jul 16 '14 at 19:17
  • 1
    I think it's okay to ask if it is DST. One cannot move from a localtime+timezone to an unambiguous UTC time without that extra info. This is because some localtimes occur twice during the transition out of DST. – Tom Jan 11 '15 at 22:59
  • @Tom: I'm very well aware of local times occurring twice, but that doesn't mean it's a good idea to specify whether or not a local time is in DST in the conversion itself. It's better - IMO - to specify a way of resolving the ambiguity *if* it's ambiguous. I prefer APIs to avoid allowing you to express impossible situations, such as EDT in January. – Jon Skeet Jan 12 '15 at 06:45
  • So you want a rule which instructs *how* to resolve it when ambiguous? What is a sensible rule? I've only seen APIs that require a declaration, rather than a rule. Examples: moment.js, pytz's localize. (In pytz, you cannot, AFAIK, create EDT in January since you specify IANA timezones, but you still have to use is_dst while localizing.) – Tom Jan 13 '15 at 08:17
  • @Tom: See my Noda Time API for an example :) The "right thing to do" really depends on the app. – Jon Skeet Jan 13 '15 at 08:18
  • 1
    If you're confused about how to do timezone in PHP, it's because PHP is a mess, here's a fantastic writeup of why php should just be retired: https://eev.ee/blog/2012/04/09/php-a-fractal-of-bad-design/ – Lucent Fox Jun 19 '17 at 17:47
2

I'm having the same issue, using this code:

date_default_timezone_set('UTC');
$nowdate = date('l jS \of F Y h:i:s A');
echo "Current date and time is: " . $nowdate;

It is summer now, and the time this code produces is one hour behind - so I don't think that UTC time in PHP adjusts to DST.

Be interesting to see if anyone has the solution...

EDIT:

Found this code on a forum, works perfectly!!

date_default_timezone_set('Europe/London');
$TIME =  date("m/d/Y H:i",time());

So you can then call the current date and time by using $TIME. The output can be adjusted to display it differently by changing the bit inside the date() tag. If you want to adjust the date() output, use this guide: http://php.net/manual/en/function.time.php

Hope this helps :)

MattFiler
  • 175
  • 2
  • 15
  • Well, that is correct then, as when you set timezone to UTC, during summer you would get a time that is one hour behind your local time (in London). https://www.timeanddate.com/time/zone/uk/london – Kaos May 20 '21 at 11:51