Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Jan 2005
    Posts
    470
    Thanks
    3
    Thanked 0 Times in 0 Posts

    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?

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    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.

  • #3
    Regular Coder
    Join Date
    Jan 2005
    Posts
    470
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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?

  • #4
    Senior Coder TheShaner's Avatar
    Join Date
    Sep 2005
    Location
    Orlando, FL
    Posts
    1,126
    Thanks
    2
    Thanked 40 Times in 40 Posts
    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

  • #5
    Regular Coder
    Join Date
    Jan 2005
    Posts
    470
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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?

  • #6
    Regular Coder
    Join Date
    May 2006
    Location
    Wales
    Posts
    820
    Thanks
    1
    Thanked 82 Times in 79 Posts
    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

  • #7
    Senior Coder TheShaner's Avatar
    Join Date
    Sep 2005
    Location
    Orlando, FL
    Posts
    1,126
    Thanks
    2
    Thanked 40 Times in 40 Posts
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •