JRL
JRL

Reputation: 78033

Getting Windows Time in PHP

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

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.

Upvotes: 1

Views: 4811

Answers (2)

mario
mario

Reputation: 145512

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

Upvotes: 2

The Surrican
The Surrican

Reputation: 29874

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

Upvotes: 2

Related Questions