Solved

PHP code to query and Echo an NTP server time

Posted on 2010-09-01
5
1,983 Views
Last Modified: 2012-06-21
I am looking for some PHP code that I can publish to a website that will query a few different NTP servers and print the results to the web page.

Example: the PHP script would check the NTP servers time.nist.gov and pool.ntp.org than echo to the web page.  Any idea how I could do this?
0
Comment
Question by:ipconfig610
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
5 Comments
 
LVL 4

Accepted Solution

by:
ricovox earned 500 total points
ID: 33577167
Take a look at the following code.
It should give you some ideas.

<?php
  // ntp time servers to contact
  // we try them one at a time if the previous failed (failover)
  // if all fail then wait till tomorrow
  $time_servers = array("time.nist.gov",
                        "nist1.datum.com",
                        "time-a.timefreq.bldrdoc.gov",
                        "utcnist.colorado.edu");

  // date and clock programs (change for your system)
  $date_app  = "/bin/date";
  $clock_app = "/sbin/clock";

  // a flag and number of servers
  $valid_response = false;
  $ts_count = sizeof($time_servers);

  // time adjustment
  // I'm in California and the clock will be set to -0800 UTC [8 hours] for PST
  // you will need to change this value for your region (seconds)
  $time_adjustment = 0;

  for ($i=0; $i<$ts_count; $i++) {
    $time_server = $time_servers[$i];
    $fp = fsockopen($time_server, 37, $errno, $errstr, 30);
    if (!$fp) {
      echo "$time_server: $errstr ($errno)\n";
      echo "Trying next available server...\n\n";
    } else {
      $data = NULL;
      while (!feof($fp)) {
        $data .= fgets($fp, 128);
      }
      fclose($fp);

      // we have a response...is it valid? (4 char string -> 32 bits)
      if (strlen($data) != 4) {
        echo "NTP Server {$time_server} returned an invalid response.\n";
        if ($i != ($ts_count - 1)) {
          echo "Trying next available server...\n\n";
        } else {
          echo "Time server list exhausted\n";
        }
      } else {
        $valid_response = true;
        break;
      }
    }
  }

  if ($valid_response) {
    // time server response is a string - convert to numeric
    $NTPtime = ord($data{0})*pow(256, 3) + ord($data{1})*pow(256, 2) + ord($data{2})*256 + ord($data{3});

    // convert the seconds to the present date & time
    // 2840140800 = Thu, 1 Jan 2060 00:00:00 UTC
    // 631152000  = Mon, 1 Jan 1990 00:00:00 UTC
    $TimeFrom1990 = $NTPtime - 2840140800;
    $TimeNow = $TimeFrom1990 + 631152000;

    // set the system time
    $TheDate = date("m/d/Y H:i:s", $TimeNow + $time_adjustment);
    $success = exec("$date_app -s \"$TheDate\"");

    // set the hardware clock (optional) - you may want to comment this out
    exec("$clock_app --systohc");

    echo "The server's date and time was set to $success\n";
  } else {
    echo "The system time could not be updated. No time servers available.\n";
  }
?>

Open in new window

0
 
LVL 4

Expert Comment

by:ricovox
ID: 33577202
0
 
LVL 4

Expert Comment

by:ricovox
ID: 33577468
I was confused by the port number used in the PHP file.
The normal port for NTP is UDP 123.
But the script uses TCP port 37, which I found out was for a similar Time protocol.
See for details:
http://www.nist.gov/physlab/div847/grp40/its.cfm
0
 
LVL 4

Expert Comment

by:ricovox
ID: 33577536
It is possible to communicate over UDP from PHP also, so if that script doesn't work for you, you can also try to use udp port 123. However the returned value may be different, and may need to be parsed differently.
0
 
LVL 4

Expert Comment

by:ricovox
ID: 33577635
After further research, I found that port 37 is for the TIME protocol, which is a simplified version of NTP. The TIME protocol returns a 32-bit number (seconds since 1900-01-01 00:00.00 UTC).
That will probably be sufficient for your needs (unless you need fractions of a second, which are not provided)
More info here: http://www.kloth.net/software/timesrv1.php

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question