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