1

How can I get the current Windows time in a PHP script?

Using time() doesn't seem to work because it is timezone aware and ignores whether DST is actually turned on or not on the server. E.g., if the timezone is set to "America/Los_Angeles" but DST is unchecked (Windows machine), if today's date is say Sept 1, then the windows time can be 1 PM but the PHP time() will return 2 PM.

How can I get the server's Windows time? (using PHP 5.2.14).

Reproducing the problem

  • Set timezone to "America/Los_Angeles" and uncheck the "Daylight Savings Time" checkbox. Set the date to Sept 1, 2010, at 16:00:00 PM.
  • Create a simple PHP script with the following, then run it

echo date_default_timezone_get(), date("H:i:s", time());

The output is America/Los_Angeles 17:00:00. Notice the 1 hour difference due to DST.

JRL
  • 76,767
  • 18
  • 98
  • 146
  • 2
    `time()` returns a timezone-neutral UNIX timestamp. How are you determining it's off by one hour? – deceze Jan 04 '11 at 23:50
  • What does `date_default_timezone_get()` say? Can you show some code? How are you determining that `time()` is off? – Pekka Jan 04 '11 at 23:58
  • `date("r")` should already give you the local time according to system settings and time zone, while `gmdate("r")` is the GMT version. Otherwise it might be a [`php.ini` issue](http://de3.php.net/manual/en/datetime.configuration.php). – mario Jan 05 '11 at 00:10
  • @JRL I think the problem is that PHP simply doesn't listen to your local DST setting. It uses the official DST info embedded in the time zone information, which says whether there is DST and during what period. Not sure what to do about this though – Pekka Jan 05 '11 at 00:33
  • @Pekka: exactly, that's what I concluded. What I need is the system time. Maybe using a system call like system(time) or something? Can this be used reliably? That's what I was hoping to find out. – JRL Jan 05 '11 at 00:35
  • A system call to get the time would be ugly. @Joe's answer should work I think – Pekka Jan 05 '11 at 00:41
  • Does `time` return a different timestamp depending on whether DST is on or off? AFAIK it shouldn't. Also, `date('...', time())` is redundant, it's the same as `date('...')`. What this leads to is that `time` is probably not the culprit, but the conversion to a human readable time format through `date` is. I don't know if that's just nitpicking or if that gives you some important information. :) – deceze Jan 05 '11 at 01:17

2 Answers2

2

date("Z") gives you the signed local offset from gmt in seconds. you can then add that to the output of date. please see the manpage of the date function

if you dont trust that settings you can also execute the date command using shell_exec and parse the output

The Surrican
  • 29,118
  • 24
  • 122
  • 168
  • date("Z") gives the timezone offset, which is not what I need since it doesn't take into account whether DST is enabled or not on the server. – JRL Jan 05 '11 at 00:47
  • As for parsing the ouput of shell_exec, the problem is that the locale can vary and so the matcher for it could be complex. – JRL Jan 05 '11 at 00:58
2

I think PHP doesn't take the Windows DST setting into account. Timezones are interpreted according to the defined standard. Also according to last time this came up: Daylight savings time not correct even after setting timezone

But see http://php.net/manual/en/function.date-default-timezone-set.php
There is some example code using WShell to query the Win registry for the actual values, including the DST flag. Then using timezone_abbreviations_list() you can find an alias with the correct timezone name and DST flag, set this.

Also @Pekka had another manual workaround here: php date() one hour ahead of server time (DST problem) (he just doesn't tell everyone.)

Community
  • 1
  • 1
mario
  • 144,265
  • 20
  • 237
  • 291