Get TIme from TimeServer

前端 未结 2 1682
终归单人心
终归单人心 2020-12-07 03:40

I am trying to get the Time from a NIST timeserver. How can I do this. I can use any language but I prefer php

相关标签:
2条回答
  • 2020-12-07 04:05

    the complete answer with real code (mix and match of code by birth and kloth):

    function query_time_server ($ntpserver, $socket) {
    /* Query a time server
       (C) 1999-09-29, Ralf D. Kloth (QRQ.software) <ralf at qrq.de> */
    
        // parameters: server, socket, error code, error text, timeout
        $fp = fsockopen($ntpserver,$socket,$err,$errstr,3);
    
        if ($fp) {
            fputs($fp,"\n");
            $timevalue = fread($fp,49);
            fclose($fp); 
        } else {
            $timevalue = " ";
        }
    
        $ret = array();
        $ret[] = $timevalue;
        $ret[] = $err;     # error code
        $ret[] = $errstr;  # error text
        return($ret);
    }
     $got_time = false;
    $servers = array(
    
        'ntp.pads.ufrj.br',
        'ntp2.pads.ufrj.br',
        'ntp3.pads.ufrj.br',
        'time.nist.gov',
        'ntp.on.br',
        'ntp2.on.br'
    );
    
    
    try { 
        foreach ( $servers as $ntpserver ) {
            echo "</br>".$ntpserver."</br>";
    
                $fp = @fsockopen( $ntpserver, 37, $errno, $errstr, 2 );
            if ( !$fp ) {
                // offline or connection refused, try next
                echo $ntpserver." is offline or connection refused, try next.</br>";
                //continue;
            } else {
    
                $data = '';
                while ( !feof( $fp ) ) {
                    $data .= fgets( $fp, 4 );
                }
                fclose( $fp );
    
                if ( strlen( $data ) == 4 ) {
                    $got_time = true;
                    $time1900 = hexdec( bin2hex( $data ) );
                    $timestamp = $time1900 - 2208988800;   // Time server is based on 1900 while Unix is based on 1970
    
                    //break;
                } else {
                    $timestamp = "NO RESULT";
                }
                echo $ntpserver." port 37 is ONLINE connection accepted, with result: ".$timestamp."</br>";
    
            }
    
    
    
            //$ntpserver = "ntp.pads.ufrj.br";
    
            $timercvd = query_time_server($ntpserver,13);
            //if ($timercvd[1] > "") { echo "urubu patata";}
    
            if (!$timercvd[1]) { // if no error from query_time_server
                $timevalue = $timercvd[0];
                echo "Time check from time server ".$ntpserver." on port 13 : <font color='red'>".$timevalue."</font></br>";
                //echo "Time check from time server ".$ntpserver." : [<font color=\"red\">".$timevalue."</font>].<br>\n";
            }  else {
                echo "Unfortunately, the time server $ntpserver could not be reached at this time on port 13. </br>";
                echo "$timercvd[1] $timercvd[2].<br>\n";
            } //*/
        }
    } catch (Exception $e) {
        echo "Exceção pega2: ",  $e->getMessage(), "\n";
    } ?>
    
    0 讨论(0)
  • 2020-12-07 04:19
    <?php
    /* Query a time server
       (C) 1999-09-29, Ralf D. Kloth (QRQ.software) <ralf at qrq.de> */
    $timeserver = "ntp1.sf-bay.org";
    $timercvd = query_time_server($timeserver,37);
    if (!$timercvd[1]) { # if no error from query_time_server
      $timevalue = bin2hex ($timercvd[0]);
      $timevalue = abs (HexDec('7fffffff') - HexDec($timevalue) - HexDec('7fffffff')) ;
      $tmestamp = $timevalue - 2208988800; # convert to UNIX epoch time stamp
      $datum = date("Y-m-d (D) H:i:s",$tmestamp - date("Z",$tmestamp)); /* incl time zone offset */
      $doy = (date("z",$tmestamp)+1);
    
      echo "Time check from time server ",$timeserver," : [<font color=\"red\">",$timevalue,"</font>]";
      echo " (seconds since 1900-01-01 00:00.00).<br>\n";
      echo "The current date and universal time is ",$datum," UTC. ";
      echo "It is day ",$doy," of this year.<br>\n";
      echo "The unix epoch time stamp is $tmestamp.<br>\n";
    } #if (!$timercvd)
    else {
      echo "Unfortunately, the time server $timeserver could not be reached at this time. ";
      echo "$timercvd[1] $timercvd[2].<br>\n";
    } # else
    ?>
    

    source

    0 讨论(0)
提交回复
热议问题