View Single Post
Old 2004-08-18, 08:58 PM   #3
Pazz
WHO IS FONZY!?! Don't they teach you anything at school?
 
Join Date: Apr 2003
Location: Australia
Posts: 42
Send a message via ICQ to Pazz
Why do you need to pack and unpack?

If you just want to query a time server this does it using port13 and port37.

Thanks to google, Ralf at qrq.de and some modifications by me.

<?php
function query_time_server ($timeserver, $socket) {
$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);
}
else {
$timevalue = " ";
}
$ret = array();
$ret[] = $timevalue;
$ret[] = $err;
$ret[] = $errstr;
return($ret);
}

$timestamp = time();
$datum = date("Y-m-d (D) H:i:s",$timestamp);
echo "Current date and local time on this server is $datum <br>\n";

$timeserver = "time-C.timefreq.bldrdoc.gov";
$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 <font color=\"red\">",$timeserver,"</font>.<br>\n"
."The current date and universal time is <font color=\"red\">",$datum,"</font> UTC.<br>\n"
."<font color=\"red\">",$timevalue,"</font> seconds since 1900-01-01 00:00.00.<br>\n"
."It is day <font color=\"red\">",$doy,"</font> of this year.<br>\n"
."The unix epoch time stamp is <font color=\"red\">$tmestamp.</font><br>\n";
}
else {
echo "Unfortunately, the time server $timeserver could not be reached at this time. "
."$timercvd[1] $timercvd[2].<br>\n";
}

$timercvd = query_time_server($timeserver,13);
if (!$timercvd[1]) {
$timevalue = $timercvd[0];
echo "Or if you prefer [<font color=\"red\">",$timevalue,"</font>].<br>\n";
}
else {
echo "Unfortunately, the time server $timeserver could not be reached at this time. "
."$timercvd[1] $timercvd[2].<br>\n";
}
?>
__________________
Artificial intelligence is no match for natural stupidity
Pazz is offline   Reply With Quote