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
https://forums.phpfreaks.com/topic/174784-getting-the-right-time/
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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.