Jump to content

Recommended Posts

quick google search returned this, not sure if you can use part of it:

 

PHP and Perl example source code

 

The following code is for demonstration purposes only. Please ensure that your web-server has set a correct local time and ask your local server's clock instead of sending time queries to distant servers via the internet.

 

<?php

$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";

?>

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.

 

<?php # PHP V4

 

function query_time_server ($timeserver, $socket) {

/* Query a time server

  © 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)

 

<?php

/* Query a time server

  © 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) :

 

<?php

/* Query a time server

  © 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

?>

Link to comment
https://forums.phpfreaks.com/topic/75538-form-questions/#findComment-382144
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.