PHP Remote School Time

Source: Internet
Author: User
There is a situation like this, we need a very reliable current time to support, although most of the server time is very accurate, we use time () to obtain the timing is reliable, but there is some uncertainty, and some of the server because it does not open NTP (Network time Protocol) to time synchronization, and because of the hardware environment and other factors, resulting in a variety of time, which in general, but also a few minutes, up to a few hours of the situation is understandable, because in the same site using the same time, but sometimes this is very bad.

On the Internet, there are free timing servers dedicated to UTC time correction, using the NTP protocol, which can refer to the previous link for more knowledge. The time error he provides is around 20ms (not to mention the time that is spent in data transfer, which NTP has already taken into account), so we can get a very credible current time with this timing server.

Let's take a look at how to get very precise UTC time with PHP, because the NTP protocol is a little bit bad, we have a better protocol to use, he is the daytime protocol protocol, through which we can directly get text data, This will be more convenient to handle. We use Time.nist.gov's time service, the code is very simple, with TCP to connect its 13 port on it.

$fp =fsockopen (' time.nist.gov ', $errno, $ERRSTR, n); Echo fread ($fp, 2096);

The data obtained are similar to this:

54979 09-05-28 09:10:50 0 0 206.9 UTC (NIST) *

Each part of this string has meaning, see: http://tf.nist.gov/service/its.htm, now we only care about the middle part and the 206.9 front of the 0, it indicates the health status of the server, also reflects the current time is trustworthy.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.