I am writing some code that needs accurate time keeping.

I was using a timestamp below to get correct time and then work off that timestamp for all other dates.

$now = time();
echo "Time Now: " . date('Y-m-d H:i:s', $now)."<br>";
echo "Timestamp: ".$now." which is: ".date('Y-m-d H:i:s', $now)."<br>";

I had a problem last week during the DST change here in the UK when the UK went from BST to UTC and I had problems.

So now I have decided to keep all my timestamps now in UTC time, this also make it easier with using the code in different time zones.

My question is how can I get the timestamp of NOW using gmmktime as the current systems I am working on are now all UTC therefore there is no difference to work with or check it the code is correct.
So which of the 2 bits of code below is accurate ready for when the time all changes again next year.

$gmnow = gmmktime();
echo "UTC Time Now: " . date('Y-m-d H:i:s', $gmnow)."<br>";
echo "UTC Seconds: ".$gmnow." which is: ".date('Y-m-d H:i:s', $gmnow)."<br>";
echo "------------------------------------------------<br>";
echo "UTC Time Now: " . gmdate('Y-m-d H:i:s', $gmnow)."<br>";
echo "UTC Seconds: ".$gmnow." which is: ".gmdate('Y-m-d H:i:s', $gmnow)."<br>";

In my eyes the code should be the bottom bit as it is using gmdate to provide the dates but I could be completly wrong and eager to find out.
Thanks

    Moving on a bit more I have tried various configurations using the following code.

    $now = time();
    echo "Time Now: " . date('Y-m-d H:i:s', $now)."<br>";
    echo "Seconds: <b>".$now." </b>which is: ".date('Y-m-d H:i:s', $now)."<br>";
    $gmnow = gmmktime();
    echo "UTC Time Now: " . date('Y-m-d H:i:s', $gmnow)."<br>";
    echo "UTC Seconds: <b>".$gmnow." </b>which is: ".date('Y-m-d H:i:s', $gmnow)."<br>";
    echo "UTC Time Now: " . gmdate('Y-m-d H:i:s', $gmnow)."<br>";
    echo "UTC Seconds: <b>".$gmnow." </b>which is: ".gmdate('Y-m-d H:i:s', $gmnow)."<br>";
    echo "-------------------------------------------------------------<br>";
    $Date2 = gmmktime(); 
    echo "gmmktime no para: ".$Date2."<br>";
    echo "Seconds: <b>".$Date2." </b>which is: ".date('Y-m-d H:i:s', $Date2)."<br>";
    echo "-------------------------------------------------------------<br>";
    $Date2 = gmmktime(date("s"), date("i"), date("H"), date("m")  , date("d"), date("Y")); 
    echo "gmmktime using date: ".$Date2."<br>";
    echo "Seconds: <b>".$Date2." </b>which is: ".date('Y-m-d H:i:s', $Date2)."<br>";
    echo "-------------------------------------------------------------<br>";
    $Date2 = gmmktime(gmdate("s"), date("i"), date("H"), date("m")  , date("d"), date("Y")); 
    echo "gmmktime using gmdate: ".$Date2."<br>";
    echo "Seconds: <b>".$Date2." </b>which is: ".date('Y-m-d H:i:s', $Date2)."<br>";
    echo "-------------------------------------------------------------<br>";
    $Date2 = mktime(); 
    echo "mktime no para: ".$Date2."<br>";
    echo "Seconds: <b>".$Date2." </b>which is: ".date('Y-m-d H:i:s', $Date2)."<br>";
    echo "-------------------------------------------------------------<br>";
    $Date2 = mktime(date("s"), date("i"), date("H"), date("m")  , date("d"), date("Y")); 
    echo "mktime with date: ".$Date2."<br>";
    echo "Seconds: <b>".$Date2." </b>which is: ".date('Y-m-d H:i:s', $Date2)."<br>";
    echo "-------------------------------------------------------------<br>";
    $Date2 = mktime(gmdate("s"), date("i"), date("H"), date("m")  , date("d"), date("Y")); 
    echo "mktime with gmdate: ".$Date2."<br>";
    echo "Seconds: <b>".$Date2." </b>which is: ".date('Y-m-d H:i:s', $Date2)."<br>";

    And this is what I got returned, whit some strange dates and times.

    Time Now: 2009-10-31 15:57:25
    Seconds: 1257004645 which is: 2009-10-31 15:57:25
    UTC Time Now: 2009-10-31 15:57:25
    UTC Seconds: 1257004645 which is: 2009-10-31 15:57:25
    UTC Time Now: 2009-10-31 15:57:25

    UTC Seconds: 1257004645 which is: 2009-10-31 15:57:25

    gmmktime no para: 1257004645

    Seconds: 1257004645 which is: 2009-10-31 15:57:25

    gmmktime using date: 1257040635

    Seconds: 1257040635 which is: 2009-11-01 01:57:15

    gmmktime using gmdate: 1257040635

    Seconds: 1257040635 which is: 2009-11-01 01:57:15

    mktime no para: 1257004645

    Seconds: 1257004645 which is: 2009-10-31 15:57:25

    mktime with date: 1257040635

    Seconds: 1257040635 which is: 2009-11-01 01:57:15

    mktime with gmdate: 1257040635
    Seconds: 1257040635 which is: 2009-11-01 01:57:15

    Why is there so much difference and this confuses me even more as to which statement is correct for working out the correct and accurate UTC in seconds.

      Well, if the server is configured to use UTC permanently there'd be no difference between gmmktime and mktime - by definition.

      time() always returns the number of seconds since 1970-01-01 00:00:00 UTC - assuming that the server's local time and timezone are correct (i.e., its system clock reports the same thing that the clock on the wall does).
      There are a couple of functions for getting the server's configured timezone and its corresponding offset (note that Europe/London is of course not the same thing as UTC).

      As for some of your weirder results you might want to check the order of your parameters 🙂

        Weedpacket;10932670 wrote:

        Well, if the server is configured to use UTC permanently there'd be no difference between gmmktime and mktime - by definition.

        time() always returns the number of seconds since 1970-01-01 00:00:00 UTC - assuming that the server's local time and timezone are correct (i.e., its system clock reports the same thing that the clock on the wall does).
        There are a couple of functions for getting the server's configured timezone and its corresponding offset (note that Europe/London is of course not the same thing as UTC).

        The server "I" shall be using it on is based in Chicago and is running UTC-0500.

        So the correct format is the

        gmmktime(gmdate("H"), gmdate("i"), gmdate("s"), gmdate("n") , gmdate("j"), gmdate("Y"));

        I presume

        Weedpacket;10932670 wrote:

        As for some of your weirder results you might want to check the order of your parameters 🙂

        Arr yes, Mins ad seconds were transposed.

          Pigmaster wrote:

          The server "I" shall be using it on is based in Chicago and is running UTC-0500.

          As long as PHP is reading this correctly then there shouldn't be a problem; the timestamp of NOW is that which is returned by [man]time/man. It is, after all, just a count of seconds that have elapsed since a certain instant. How they get labelled when you display them in hours minutes and seconds is what depends on the timezone.

          $zones = array(
          	'UTC' => 'UTC',
          	'Chicago' => 'America/Chicago',
          	'London' => 'Europe/London',
          	'Santiago' => 'America/Santiago',
          	'Perth' => 'Australia/Perth',
          	'Auckland' => 'Pacific/Auckland',
          );
          
          $clocks = array();
          foreach($zones as $place=>$zone)
          {
          	$clocks[$place] = new DateTime("now", new DateTimeZone($zone));
          }
          foreach($clocks as $place=>$time)
          {
          	echo $place,": ", $time->format('Y-m-d H:i:s O'), ($time->format('I') ? ' (DST)' : ''), "\n\n";
          }
          
            Write a Reply...