0

Just wondering if there is a way in PHP that can retrieve the time from a specific server (for example google). I want to make a script that can retrive the time that is set on the server.

Thanks :)

0

2 Answers 2

4

If you are OK with trusting the Date: header returned by an HTTP request, you can do something like this with cURL in PHP.

<?                                                                                                                                  
$date = null;

function header_callback($curl, $header)
{
    global $date;

    if (preg_match('/^Date:/', $header)) {
        $date = trim(substr($header, 5));
        $date = DateTime::createFromFormat('D, d M Y H:i:s e', $date);
    }

    return strlen($header);
}

$curl = curl_init("http://www.google.com/");

curl_setopt($curl, CURLOPT_NOBODY, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADERFUNCTION, 'header_callback');

curl_exec($curl);

curl_close($curl);

if ($date != NULL) {
    echo "Date from HTTP: " . $date->format('r');
}
?>
Sign up to request clarification or add additional context in comments.

Comments

1

There is no built in PHP function to query a particular time server, but you can write a script that will do it. Some examples can be found on this page: http://www.kloth.net/software/timesrv1.php

This example from the previous link uses the TIME protocol on port 37:

<?php
/* Query a time server
   (C) 1999-09-29, Ralf D. Kloth (QRQ.software) <ralf at qrq.de> 
*/

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); # close the connection
  }
  else {
    $timevalue = " ";
  }

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

} # function query_time_server 

$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
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.