How to do take time from a time server using php

Post Reply
User avatar
Saman
Lieutenant Colonel
Lieutenant Colonel
Posts: 828
Joined: Fri Jul 31, 2009 10:32 pm
Location: Mount Lavinia

How to do take time from a time server using php

Post by Saman » Thu Aug 18, 2011 12:32 am

The DAYTIME protocol, RFC-867
The DAYTIME server protocol is described in RFC-867.

The time server is listening on TCP/UDP port 13 and sends an ASCII string (one line, no format defined).

This is the time protocol sent by the NIST time servers, like time-b.timefreq.bldrdoc.gov, or the USNO servers, like ntp2.usno.navy.mil.

The NIST format : is JJJJJ YR-MO-DA HH:MM:SS TT L H msADV UTC(NIST) OTM.
Example: 53746 06-01-11 21:28:49 00 0 0 266.1 UTC(NIST) *
JJJJJ - the Julian Date is : 53746
YR-MO-DA - the Date is : 2006-01-11
HH:MM:SS - the Time in UTC is : 21:28:49
TT - Indication whether USA is on Standard Time (ST) or Daylight Saving Time (DST) : 00 (Standard Time)
L - Leap second at the end of the month (0: no, 1: +1, 2: -1) : 0
H - Health of the server (0: healthy, >0: errors) : 0
msADV - time code advanced in milliseconds to compensate network delays : 266.1
UTC(NIST) - the originator of this time code : UTC(NIST)
OTM - on-time marker : *

The USNO servers returns a string in the format : W Mon DY HH:MM:SS YR.

The TIME protocol, RFC-868
The TIME protocol is described in RFC-868.

The time server is listening on TCP/UDP port 37 and sends a 32-bit binary number (seconds since 1900-01-01 00:00.00 UTC).

This base will serve until time stamp 4294967295, which will be on 2036-02-07 06:28.14 UTC.

Example: 3346003716 , which translates to 2006-01-11 21:28:49.

The standardized data format refers to UTC (Coordinated Universal Time), no other time zones. This protocol cannot estimate network delays or report additional information.
Note: Time stamp on 1970-01-01 00:00.00 UTC (begin of the unix epoch) was 2208988800.


How to do time server queries
Open a socket connection to the time server on port 13 (daytime) or 37 (time), send an empty string (newline), read the result and close the connection. Then look at the result and display.

The PHP-code is easy and should be self-explaining.

Code: Select all

<?php # PHP V4

function query_time_server ($timeserver, $socket) {
/* Query a time server
   (C) 1999-09-29, Ralf D. Kloth (QRQ.software) <ralf at qrq.de> */

  $fp = fsockopen($timeserver,$socket,$err,$errstr,5);
        # parameters: server, socket, error code, error text, timeout
  if ($fp) {
    fputs($fp,"\n");
    $timevalue = fread($fp,49);
    fclose($fp); # close the connection
  }
  else {
    $timevalue = " ";
  }

  $ret = array();
  $ret[] = $timevalue;
  $ret[] = $err;     # error code
  $ret[] = $errstr;  # error text
  return($ret);

} # function query_time_server 
?>
Query a time server on port 13 (DAYTIME protocol)

Code: Select all

<?php
/* Query a time server
   (C) 1999-09-29, Ralf D. Kloth (QRQ.software) <ralf at qrq.de> */
$timeserver = "time-C.timefreq.bldrdoc.gov";
$timercvd = query_time_server($timeserver,13);
if (!$timercvd[1]) { # if no error from query_time_server
  $timevalue = $timercvd[0];
  echo "Time check from time server ",$timeserver," : [<font color=\"red\">",$timevalue,"</font>].<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
?>

Query a time server on port 37 (TIME protocol) :

Code: Select all

<?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
?>
Short list of time servers
time.nrc.caports 37, NTP
ptbtime1.ptb.deports 13 (local time), 37, NTP
ptbtime2.ptb.deports 13 (local time), 37, NTP
ntp0.fau.deports 37, NTP
ntps1-0.cs.tu-berlin.deports 13 (local time), 37, NTP
ntps1-1.cs.tu-berlin.deports 13 (local time), 37, NTP
ntps1-0.uni-erlangen.deports 37, NTP
ntp-p1.obspm.frports 13, 37, NTP
time.ien.itports 13 (local time), 37
ntp.iriti.cnr.itports 13 (local time), NTP
NIST - US National Institute of Standards and Technology - http://www.boulder.nist.gov/
time-a.timefreq.bldrdoc.govports 13, 37
time-b.timefreq.bldrdoc.govports 13, 37
time-c.timefreq.bldrdoc.govports 13, 37
time-d.timefreq.bldrdoc.govports 13, 37
USNO - U.S. Naval Observatory - http://www.usno.navy.mil/, tycho.usno.navy.mil/ntp.html
tick.usno.navy.milports 13, 37, NTP
tock.usno.navy.milports 13, 37, NTP
ntp2.usno.navy.milports 13, 37, NTP
gnomon.cc.columbia.eduports 13, 37, NTP
tick.gatech.eduports 13, 37, NTP
Post Reply

Return to “PHP & MySQL”