I have experience with the time() and date() functions, but they return the time on my server. What's the best way to get the unix-epoch timestamp for GMT time?

i'm doing something like this:

$now = time();
$utc_time = $now - intval(date('Z', $now));
echo 'NOW:' . date('m d, Y \a\t H:i:s', $now) . '<br>';
echo 'UTC TIME:' . date('m d, Y \a\t H:i:s', $utc_time);

Also, what is the difference between GMT and UTC?

    Why not use gmdate?

    GMT and UTC is the same

      sidsel wrote:

      Why not use gmdate?

      GMT and UTC is the same

      gmdate() returns a string...i need an integer.

      I tried gmmktime() but was getting some WEIRD stuff. Anybody know why? This code:

      <?
      echo 'time zone offset of this machine in seconds:' . date('Z') . '<br>';
      echo 'time zone offset of this machine in hours:' . intval(date('Z'))/3600 . '<br>';
      
      $now = time();
      echo 'right now on this machine which is in New Jersey:' . date('Y-m-d H:i:s', $now) . '</p>';
      
      $utc_time = $now - intval(date('Z', $now));
      echo 'utc_time:' . $utc_time . '<br>';
      echo 'utc time date:' . date('Y-m-d H:i:s', $utc_time) . '</p>';
      
      $gm_now = gmmktime();
      echo 'gmmktime:' . $gm_now . '<br>';
      echo 'which is:' . date('Y-m-d H:i:s', $gm_now) . '<br>';
      
      echo 'gm date:' . gmdate('Y-m-d H:i:s') . '<br>';
      ?>
      

      Has this output:

      time zone offset of this machine in seconds:-18000
      time zone offset of this machine in hours:-5
      right now on this machine which is in New Jersey:2006-11-17 00:42:22
      
      utc_time:1163760142
      utc time date:2006-11-17 05:42:22
      
      gmmktime:1163724142
      which is:2006-11-16 19:42:22
      gm date:2006-11-17 05:42:22
      

      I'm thinking maybe I don't understand gmmktime(). Or it's buggy. or something.

      Weedpacket, I just know you're trying to make me cry. Not because you're mean but because you like to expose just how far short of reality our oversimplifications are. I've always pretty much blocked out the whole Gregorian/Julian/whatever nonsense. When they say Ceaser died on the Ides of March that's good enough for me.

      But for my current chore, I need to know that my code will return the same value regardless of what it's local time is or whether it observes DST. Let's assume that the time on the server is set accurately/appropriately for the local time zone. The reason for all this is that some server in India wants to refer users to my machine with some authorization credentials including the current time. I want to make sure credentials expire after a short amount of time so we need a common concept of time accurate to approximately 1 minute.

        • time() - returns the (integer) number of seconds since 1970/01/01-00:00:00 GMT/UTC, a.k.a. "UNIX Time", which will be the same regardless of the server's time zone

        • date() - returns a formatted string for the local time zone based on the 2nd parameter's UNIX Time value, or the current time if not specified

        • gmdate() - like date(), but returns a string for the GMT/UTC time of the supplied UNIX Time, or the current time if not specified

          As NogDog notes, timestamps are always relative to 1970-01-01 00:00:00 GMT (yes, I know it's pedantry, but the name didn't change to UTC until 1972....), so at any given moment the server in India and the server in New Jersey would produce the same timestamp just so long as their clocks weren't actually wrong.

          And this is of course precisely why servers need to have their timezones configured in the first place 🙂

            so that means the computer sort of works like this then:

            Dave:  time();
            Hal9000:  That would be 979309800 seconds since January 1st 00:00:00, 1970 GMT, Dave.
            Dave:  What?  Oh, I mean date('F j, Y \a\t h:i a');
            Hal9000:  That would be January 12, 2001 at 8:30 am there in Los Angeles, Dave.
            Dave:  Oh happy birthday, Hal!  Say, what time is it in Greenwich, England?
            Hal9000:  Thank you for the birthday wishes, Dave.  It is 4:30 pm, in Greenwich, Dave.
            

            Am I to understand then that the arguments to mktime() are assumed to be local to my computer's time zone and the the server takes these arguments, and adjusts them by my time zone offset to return the number of seconds since January 1, 1970 00:00:00 in Greenwich (corresponding to 4:30pm in the example rather than 8:30 am which is the local time)?

              Yes: mktime()'s arguments are for the local time, while gmmktime()'s args are the GMT/UTC date/time values.

                PS: As an alternative to the mktime functions, if you have the date/time in a string format which strtotime() recognizes, you could just do:

                $timeString = "2004-03-01 00:21:42UTC";
                $time = strtotime($timeString);
                printf("Local time: %s<br>UTC time: %s", date('Y/m/d-H:i:s', $time), gmdate('Y/m/d-H:i:s', $time));
                
                  Write a Reply...