Jump to content

Getting the right time


grissom

Recommended Posts

Hi all

 

I've been using the PHP time() function for a while now to time-stamp various bits of activity, but of course it relies on the server clock being set correctly, which on many servers it isn't.

 

So my question is - is there a resource somewhere on the internet which I can access directly through my PHP code which will give me a "guaranteed" exact time, rather than rely on the server clock.

 

Many thanks.

Link to comment
Share on other sites

If you want to do it via PHP

function query_time_server ($timeserver, $port) {
$timevalue = null;
$fp = fsockopen($timeserver, $port, $err, $errstr, 5);
if ($fp) {
	fputs($fp,"\n");
	$timevalue = fread($fp,49);
	fclose($fp);
}

$ret = array( $timevalue,
			  $err,			//	error code
			  $errstr		//	error text
			);
return($ret);
}	//	function query_time_server()


$timeserver = "time-C.timefreq.bldrdoc.gov";
$timercvd = query_time_server($timeserver,13);

if (!$timercvd[1]) {
$timevalue = $timercvd[0];
echo 'Time check from time server ',$timeserver,' : [<font color="red">',$timevalue,'</font>]';
} else {
echo 'Unfortunately, the time server $timeserver could not be reached at this time. ';
echo $timercvd[1].' '.$timercvd[2];
}

You can change the timeserver to any public ntp server

Link to comment
Share on other sites

It will undoubtedly slow down his web pages if he has to wait for the response of an external third party each time.
True, but perhaps the timeserver script could be run periodically as a cron task, and the result used to set the system clock via an exec.... assuming that's permitted by the host configuration
Link to comment
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.