grissom Posted September 19, 2009 Share Posted September 19, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/174784-getting-the-right-time/ Share on other sites More sharing options...
Daniel0 Posted September 19, 2009 Share Posted September 19, 2009 Tell your server admin to install an NTP client and adjust the time zone manually in PHP as described in this tutorial: http://www.phpfreaks.com/tutorial/working-with-dates-in-php Quote Link to comment https://forums.phpfreaks.com/topic/174784-getting-the-right-time/#findComment-921114 Share on other sites More sharing options...
Mark Baker Posted September 19, 2009 Share Posted September 19, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/174784-getting-the-right-time/#findComment-921118 Share on other sites More sharing options...
Daniel0 Posted September 19, 2009 Share Posted September 19, 2009 It will undoubtedly slow down his web pages if he has to wait for the response of an external third party each time. Quote Link to comment https://forums.phpfreaks.com/topic/174784-getting-the-right-time/#findComment-921120 Share on other sites More sharing options...
Mark Baker Posted September 19, 2009 Share Posted September 19, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/174784-getting-the-right-time/#findComment-921123 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.