Forum Moderators: coopster

Message Too Old, No Replies

Query date server on the Web?

         

DrDoc

7:36 pm on Oct 29, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Does anyone know of a Web service you can query to get the accurate current date/time? I'd like to use that in a PHP script...

coopster

7:47 pm on Oct 29, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



[boulder.nist.gov...]
or better yet, see the servers list here:
[ntp.org...]

[edited by: coopster at 7:50 pm (utc) on Oct. 29, 2003]

bcolflesh

7:48 pm on Oct 29, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I pull stuff from here when necessary:

worldtimeserver.com/

Romeo

7:57 pm on Oct 29, 2003 (gmt 0)

10+ Year Member



... if you want to get a date/time from the internet, there are several protocols to choose from:
For a start you can query (php fsockopen()) time-b.timefreq.bldrdoc.gov on port 13 (DAYTIME protocol, RFC867) and get a string in the format
JJJJJ YR-MO-DA HH:MM:SS TT L H msADV UTC(NIST) OTM
Another simple time protocol is the TIME protocol (RFC868).
SNTP (RFC2030) and even NTP (RFC1305) are more sophisticated and require extensive reading the specs.

The far best possibility is to keep your own web server's time accurate, either in sync with NTP (xntpd) or just by a daily update with ntpdate, and then just ask for the time locally:
<?php echo gmdate("Y-m-d H:i.s");?>

Regards,
R.

coopster

9:50 pm on Oct 29, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



First, DrDoc, here is a snippet to get the info you requested using the Daytime Protocol (RFC-867) as suggested by Romeo (Note: you can find a similar example in the PHP Manual under fsockopen function [us3.php.net] -- I added the REGEX to get the date/time out):

<?php
$handle = fsockopen("time-b.timefreq.bldrdoc.gov", 13, $errno, $errstr);
if (!$handle) {
print "ERROR: $errno - $errstr<br>\n";
} else {
$daytime = fread($handle, 50);
fclose($handle);
if (preg_match("/\s+\d+-\d+-\d+\s+/", $daytime, $matches)) $date = $matches[0];
if (preg_match("/\s+\d+:\d+:\d+\s+/", $daytime, $matches)) $time = $matches[0];
}
print $daytime.'<br >';
print $date.'<br >';
print $time.'<br >';
?>

Now, while the topic is open...

According to NIST [boulder.nist.gov],

The Network Time Protocol (NTP) is the most commonly used Internet time protocol, and the one that provides the best performance.

Has anybody ever coded an NTP query in PHP?

Romeo

10:55 pm on Oct 29, 2003 (gmt 0)

10+ Year Member



Coopster, thanks for sharing the code.
While NTP itself is a sophisticated time synchronisation protocol, more suited for longrunning daemons in the background than for a PHP foreground script, simple SNTP lookups against NTP servers could be done.
I once have written an SNTP lookup in perl, which should be translatable into PHP. The perl code is too much to be posted here, but can be easily found via google.

Regards,
R.

coopster

11:22 pm on Oct 29, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Thanks, Romeo, found your code, along with the rest of your research and work on the date and time protocols and getting the information from time servers. Seems you've done quite a bit of work in this area, a lot of work by the looks of it.

Someday, in my spare time, I'll dig deeper into SNTP and your Perl code and work on a PHP conversion. In the meantime I'll use the DAYTIME protocol -- when I need it ;)

Thanks again -- coopster

DrDoc

6:01 pm on Oct 30, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks everyone for your replies. :)
Very helpful