Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-28-2007, 04:21 PM   PM User | #1
bauhsoj
Regular Coder

 
Join Date: Jan 2005
Posts: 470
Thanks: 3
Thanked 0 Times in 0 Posts
bauhsoj is an unknown quantity at this point
Problem with date() being off by 23 hours

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?
bauhsoj is offline   Reply With Quote
Old 06-28-2007, 04:26 PM   PM User | #2
Fumigator
UE Antagonizer


 
Fumigator's Avatar
 
Join Date: Dec 2005
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 7,686
Thanks: 42
Thanked 637 Times in 625 Posts
Fumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of light
It all depends on the time zone the server is sitting in... you'll want to use Greenwich time (GMT) via the gmtime() function if you want a consistent result.
__________________
Fumigator is offline   Reply With Quote
Old 06-28-2007, 04:52 PM   PM User | #3
bauhsoj
Regular Coder

 
Join Date: Jan 2005
Posts: 470
Thanks: 3
Thanked 0 Times in 0 Posts
bauhsoj is an unknown quantity at this point
Quote:
Originally Posted by Fumigator View Post
It all depends on the time zone the server is sitting in... you'll want to use Greenwich time (GMT) via the gmtime() 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?
bauhsoj is offline   Reply With Quote
Old 06-28-2007, 05:21 PM   PM User | #4
TheShaner
Senior Coder

 
TheShaner's Avatar
 
Join Date: Sep 2005
Location: Orlando, FL
Posts: 1,125
Thanks: 2
Thanked 40 Times in 40 Posts
TheShaner will become famous soon enoughTheShaner will become famous soon enough
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 to set the default timezone before running your date scripts.

-Shane
TheShaner is offline   Reply With Quote
Old 06-28-2007, 06:06 PM   PM User | #5
bauhsoj
Regular Coder

 
Join Date: Jan 2005
Posts: 470
Thanks: 3
Thanked 0 Times in 0 Posts
bauhsoj is an unknown quantity at this point
Quote:
Originally Posted by TheShaner View Post
Try using the 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?
bauhsoj is offline   Reply With Quote
Old 06-28-2007, 07:01 PM   PM User | #6
Mwnciau
Regular Coder

 
Join Date: May 2006
Location: Wales
Posts: 820
Thanks: 1
Thanked 82 Times in 79 Posts
Mwnciau is on a distinguished road
PHP Code:
<?
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
Mwnciau is offline   Reply With Quote
Old 06-28-2007, 07:03 PM   PM User | #7
TheShaner
Senior Coder

 
TheShaner's Avatar
 
Join Date: Sep 2005
Location: Orlando, FL
Posts: 1,125
Thanks: 2
Thanked 40 Times in 40 Posts
TheShaner will become famous soon enoughTheShaner will become famous soon enough
Well, there are two solutions that I know of:

Use my function below (not tested tho):
PHP Code:
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() like so:
PHP Code:
putenv('TZ=US/Eastern'); 
-Shane
TheShaner is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:30 AM.


Advertisement
Log in to turn off these ads.