views:

558

answers:

4

On a Win2k server, using a WAPP stack (Windows,Apache,PostgreSQL,PHP). Running a PHP web application. Also tested using PHP interactive console.

The datetime is reported correctly, but for some strange reason the timezone is reported incorrectly:

php -a
<?php
echo date('c');
2009-04-19T16:52:35-04:00


C:\WAPP\php>time /t
17:06

C:\WAPP\php>date /t
Sun 04/19/2009

The server's OS timezone is actually set to GMT-5. I used an (unofficial) DST fix and also tried to use php_timezonedb.dll extension, but the problem persists.

This server is also running other applications (not under my control), so I can't just change the time. I could also just temporarily set everyone to GMT-4 in the PHP application, but I feel that there should be a less hackish solution.

Does anyone have any suggestions, or can give a more in depth explanation of what is happening? I suspect that it has something to do with the recent changes in DST and Windows, and it is causing PHP to detect the timezone improperly.

A: 

Unless you have access to the php.ini file (you can set the server's timezone there), you'll have to set the timezone in the script.

Ian
+1  A: 

You could avoid a lot of the timezone related issues by using:

gmdate  ( string $format  [, int $timestamp  ] );

It is identical to the date() function except that the time returned is in GMT (Greenwich Mean Time).

Jacco
A: 

You can set the timezone php is using by setting the php date.timezone variable in a .htaccess in the application directory.

php_value date.timezone "America/Bahia"
Peter Smit
A: 

You have to set the timezone (either from the PHP.ini or with date_default_timezone_set())

I'm guessing if you turn on Strict error notification, you'd get an error about not setting the default timezone.

Every call to a date/time function will generate a E_NOTICE if the time zone is not valid, and/or a E_STRICT message if using the system settings or the TZ environment variable. See also date_default_timezone_set()

Ólafur Waage