PHP gmmktime() not giving the right time

Discussion in 'Web Design and Development' started by big_malk, Sep 8, 2008.

  1. big_malk macrumors 6502a

    Aug 7, 2005
    I'm using a shared Linux server running PHP / MySQL and it is in the US.
    Currently, mktime() is giving a time almost 10am, but because I'm in the UK I'm trying to use gmmktime(), but it's giving a time of 5am, while it's actually just gone 4pm!

    Since it's a shared server, I don't think there's much I can do about the time settings? Is it time to ask the server support people for advice, or am I missing something?
  2. SrWebDeveloper macrumors 68000


    Dec 7, 2007
    Alexandria, VA, USA
    That function produces a Unix timestamp based on GMT time of the server in the United States, not the local time set there. This timestamp is good for database storage if that's what you're really trying to do. After retreival from the database, simply convert the GMT timestamp to local by adding or subtracting the number of hours offset from GMT based on their local time zone. Be sure to account for DST as well, the extra hour matters in certain parts of the year. The user will then see their local time and zone.

    From the PHP docs:

    So it is important the local time be correct, of course since the GMT calculations are based on local time. I doubt that's the issue - alot of users would be complaining, big time.

  3. fabian9 macrumors 65816


    Nov 28, 2007
    Bristol, UK
    I found this on the PHP manual on

    gmmktime() should ONLY be used to create a timestamp when specifying a specific GMT date and time.

    If you want to make a valid time stamp for the current date & time, use mktime() instead.

    UNIX timestamps, by definition, store the GMT time relative to the UNIX epoch.

    gmmktime() (without any parameters specified) will effectively use the computer's LOCAL time values just the same as if they were explicit parameters, and the resulting time stamp will be incorrect. (The resulting timestamp will actually be offset in the OPPOSITE direction of the local timezone offset from GMT!)

    And this will probably also help:

    Beware that despite the documentation which states is_dst is ignored, with PHP 5.2 at least, it is not actually ignored and will cause a 1 hour offset on the UTC time returned.

    This caused some interesting bugs, especially with the tzdelta function shown from previous posts below - you need to make the final parameter a 0 instead of $ar[8] otherwise you get an off-by-1-hour as a result.

    As a result, I now use :

    function tzdelta ( $iTime = 0 ) {
    if ( 0 == $iTime ) { $iTime = time(); }
    $ar = localtime ( $iTime );
    $ar[5] += 1900; $ar[4]++;
    $iTztime = gmmktime ( $ar[2], $ar[1], $ar[0], $ar[4], $ar[3], $ar[5], 0);
    return ( $iTztime - $iTime );

    Hope it helps anyways.... Fabian
  4. big_malk thread starter macrumors 6502a

    Aug 7, 2005
    Actually, that just confused me and I'm not certain what it's trying to do lol

    I have discovered the wonders of of gmdate(), which I should have found before (duh) but I'm still not getting the accurate time, it's 1 hour behind. gmdate('I') is returning 0, indicating that daylight saving (aka 'british summertime') is not active, which would explain it!

    I can't find anything I can do to change this?

Share This Page