...

View Full Version : Problem with date() being off by 23 hours



bauhsoj
06-28-2007, 05:21 PM
I have the Unix time stamp "1184616000" which I place into date('F j, Y h:i:s A', 1184616000) and I have tested it on two servers which produce "July 17, 2007 12:00:00 AM" but on some other servers it produces "July 16, 2007 01:00:00 PM". Since it is a pregenerated timestamp the date() output should be identical regardless of where it is executed.

I am trying to figure out which is right and which is wrong. Can some people please try this out and let me know what you get?

Fumigator
06-28-2007, 05:26 PM
It all depends on the time zone the server is sitting in... you'll want to use Greenwich time (GMT) via the gmtime() (http://us.php.net/manual/en/function.gmdate.php) function if you want a consistent result.

bauhsoj
06-28-2007, 05:52 PM
It all depends on the time zone the server is sitting in... you'll want to use Greenwich time (GMT) via the gmtime() (http://us.php.net/manual/en/function.gmdate.php) function if you want a consistent result.

When I run strtotime('July 17, 2007') I get "1184616000" which if put into gmdate() gives me "July 16, 2007 08:00:00 PM" versus sticking it into date() which produces "July 17, 2007 12:00:00 AM" which is the date I want.

So how do I get gmdate() to give me the correct date from the Unix timestamp if that is the only function I can use to get accurate results across different servers?

TheShaner
06-28-2007, 06:21 PM
The timestamp you're using, 1184616000, is the time in seconds since Jan. 1, 1970 in YOUR time zone, which is 4 HOURS AHEAD of Greenwich time (GMT).

Fumigator was saying that you were getting different dates with that timestamp because the servers were using different time zones. If you use the gmdate() function, all the servers will report back with GMT time. If you use date(), they'll report back with their own timezones. So what you're really looking for is every server to report back with YOUR time zone.

Try using the date_default_timezone_set (http://php.net/date_default_timezone_set) to set the default timezone before running your date scripts.

-Shane

bauhsoj
06-28-2007, 07:06 PM
Try using the date_default_timezone_set (http://php.net/date_default_timezone_set) to set the default timezone before running your date scripts.

Regrettably one of the servers is running software that is incompatible with PHP 5.1 which is what is required by that set of functions. Is there a solution you can recommend for an earlier version?

Mwnciau
06-28-2007, 08:01 PM
<?
echo "Original Time: ". date("h:i:s")."\n";
putenv("TZ=US/Eastern");
echo "New Time: ". date("h:i:s")."\n";
?>

list of timezones:
http://www.theprojects.org/dev/zone.txt

TheShaner
06-28-2007, 08:03 PM
Well, there are two solutions that I know of:

Use my function below (not tested tho):

function get_my_local_timestamp()
{
$my_GMT_offset = -4; // Set this to however many your GMT offset is
$server_GMT_offset = intval(date("O")) / 100;
$offset_difference = $server_GMT_offset - $my_GMT_offset;
$my_local_timestamp = time() + ($offset_difference*60*60);
return $my_local_timestamp;
}
date('F j, Y h:i:s A', get_my_local_timestamp())
Alternatively, you can set the TZ environment variable using putenv() (http://php.net/putenv) like so:

putenv('TZ=US/Eastern');
-Shane



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum