I read a lot of StackOverflow questions on the javascript new Date()
object when it comes to daylight saving time lines - like when crossing. However, I did not see an answer about this particular problem or a way around it, still relying on "unix time".
I personally decided to work around this problem by passing the javascript date as the date for my PHP code, not the unix time. However, the grumbling question still remains! I have confirmed this behavior in IE8, Chrome and FF, so I assume that it will behave the same for you. ( Update: OSX users may not generate this behavior)
My research; closest questions to my problem:
- This user worked on a specific watch right around the DST change.
- This user was worried about the time representation depending on the user's time zone. The accepted answer on this page mentioned that
getTimezoneOffset
is "flaky", which caused me not to delve into it. - See my answer below for other questions that contain some great ideas.
I created a test case around November 1, 2006. This may or may not work the same for you, depending on what time zone you are in. If I understand the javascript aspect correctly, you will need to synchronize your PC clock to
Eastern time (USA and Canada) and check the box "Automatically set the clock for daylight saving time."
I base this experiment on the Indianapolis timezone in PHP. My javascript result is when I find that the unix time on November 1, 2006 is one hour of what PHP is generating (3600 seconds). According to this page (thanks Jon!) Javascript is incorrect.
The results of the two languages are returned back to the agreement 06/11/2006!
This and other studies make me believe that Javascript misunderstood its story and chose the wrong Sunday to “backtrack” from the DST, thereby causing the inconsistency that I see.
I tried to simplify this as much as possible, but there are a few more gears in motion.
1) Here is the PHP code and the output , which shows the CORRECT number of milliseconds before the date 11/01/2006.
date_default_timezone_set("America/Indiana/Indianapolis"); echo "Unixtime for November 1, 2006 is: ".strtotime("11/01/2006")."\n"; echo "Unixtime for November 6, 2006 is: ".strtotime("11/06/2006");
Result:
Unixtime for November 1, 2006 is: 1162357200 (where the disagreement lies) Unixtime for November 6, 2006 is: 1162789200
Both are based on GMT-0500.
2) In Javascript (see my jsfiddle **), I call new Date
and then getTime()
on it (and delete the milliseconds):
new Date("2006/11/01").getTime()/1000 new Date("2006/11/06").getTime()/1000
It generates values
1162353600 <-- PHP outputs: 1162357200 1162789200 <-- the same as PHP for '2006/11/06'; congruence is restored
which is the difference (for 2006/11/01) from exiting through PHP 3600 seconds - or one hour. This value (an hour earlier) when processing my PHP application generated the previous day (2006/10/31), which was unacceptable, since it disrupted my navigation forward. ( see a more detailed description of my specific scenario )
The output in Javascript: Date("2006/11/01")
without calling getTime()
clears some secrets because javascript shows the offset that it used GMT-0400
.
My jsfiddle ** experiment (also listed above) shows these results.
** (you may need to change the time zone of your computer to see identical behavior).
Perhaps the entry into the game is that according to Wikipedia 2006 was the first year Indiana started using DST. A curious puzzle anyway.
Since I already developed my solution (avoiding relying on unix time in my javascript), I thought I should post it for posterity and hopefully someone can know how to fix the javascript value.
The question is:. How can you bring the two unix time results between PHP and javascript into alignment? I would appreciate to know how around DST 'line' or in general. (I currently assume that the DST line is a problem).
Update: iMac working with Chrome generates the results that PHP generates. What kind??? Wild. Any javascript-side related fix seems to be a great event (or at least ugly). Perhaps this is not a problem with javascript, as it generates the correct answer depending on the OS (or other factors?).
It is noteworthy that on this iMac I did not have to force the time zone, and I'm not sure that this Apple computer will allow this. The settings "Set date and time automatically" ( true
) and disabled
were noted. The time zone was set to Eastern Daylight Time. The "Set time zone automatically" ( false
) and disabled
.
I added a Windows
tag to emphasize that this does not seem to be a problem in OSX.
Update: In accordance with the site linked above , I confirmed that all of the following dates intersect in the new GMT offset for the corresponding dates (updated fiddle) - unlike the 2006 response for a week. Ie, November 4, 2007, he was GMT-4
and November 5, 2007, he returned GMT-5
.
- 2007 Sunday, March 11 at 2:00 a.m. Sunday, November 4 at 2:00 a.m.
- 2008 Sunday, March 9 at 2:00 a.m. Sunday, November 2 at 2:00 a.m.
- 2009 Sunday, March 8 at 2:00 on Sundays, November 1 at 2:00.
- 2010 Sunday, March 14 at 2:00 a.m. Sunday, November 7 at 2:00 a.m.
- 2011 Sunday, March 13 at 2:00 on Sundays, November 6 at 2:00.
Finally, if you know the correct channels to send this error in 2006 to any time source referenced by Javascript, do this and let me know.