php gets time (system time and network time)

WBOY
Release: 2016-07-29 09:15:00
Original
2189 people have browsed it

When using php, you may often use standard time. There are two methods.

The first method:

Get the server time

First configure date.timezone = PRC in php.inior add date_default_timezone_set("PRC"); directly on the program page. Then you can use time ()ordate(''Y-m-d H:i:s)

The second method: get the network time (here is borrowed from the Internet, original address: http://justcoding.iteye.com/blog/843645 )

There is a situation like this, such as the booking process in the air ticket business. We need a very reliable current time to support it. Although the time of most servers is very accurate, we use time() to obtain it. The time is reliable, but there may be inaccuracies. Some servers do not enable ntp (Network Time Protocol) for time synchronization, and due to factors such as the hardware environment, the time may vary. In general, there is only one difference. A few minutes or at most a few hours is understandable because the same time is used on the same website, but sometimes this is bad. On the Internet, there are free timing servers that specifically provide UTC time correction, using the NTP protocol. You can refer to the previous link for more knowledge. The time error he provides is about 20ms (not to mention the time that will be consumed during the data transmission process, NTP has long considered this), so through this timing server, we can get a very reliable current time.

Let’s now take a look at how to get a very accurate UTC time using PHP. Since the NTP protocol is a little bit difficult to use, we have a better protocol to use, which is the Daytime Protocol. Through this protocol We can get the text data directly, which will be more convenient to process. We use the time service of time.nist.gov. The code is very simple. Just use TCP to connect to its port 13.

<code><span>$fp</span>=fsockopen(<span>'time.nist.gov'</span>,<span>13</span>,<span>$errno</span>,<span>$errstr</span>,<span>90</span>);  
<span>echo</span> fread(<span>$fp</span>,<span>2010</span>);  </code>
Copy after login

The data obtained is similar to this:

<code>55545 10<span>-12-15</span> 21<span>:40</span><span>:47</span> 00 0 0 113<span>.1</span><span>UTC</span>(<span>NIST</span>) *  </code>
Copy after login

Every part of this string is meaningful. For details, please see: http://www.nist.gov/pml/div688/grp40/its.cfm, Now we only care about the middle part and the 0 in front of 206.9, which represents the health status of the server and also reflects whether the current time is trustworthy.

OK, use PHP to obtain accurate UTC time. This application is still very valuable in situations where time requirements are very strict. At this point, my problem is solved.

Now that we know the UTC standard time, the only thing left is to convert it into world time. See php to get the local time time zone

<code><span>$fp</span>=fsockopen(<span>'time.nist.gov'</span>,<span>13</span>,<span>$errno</span>,<span>$errstr</span>,<span>90</span>);  
<span>$ufc</span> = explode(<span>' '</span>,fread(<span>$fp</span>,date(<span>'Y'</span>)));  
<span>$date</span> = explode(<span>'-'</span>,<span>$ufc</span>[<span>1</span>]);  
<span>$processdate</span> = <span>$date</span>[<span>1</span>].<span>'-'</span>.<span>$date</span>[<span>2</span>].<span>'-'</span>. date(<span>'Y'</span>).<span>' '</span>.<span>$ufc</span>[<span>2</span>];  

<span>switch</span>(<span>$ufc</span>[<span>5</span>])  
{  
    <span>case</span><span>0</span>: <span>echo</span><span>'精确'</span>; <span>break</span>;  

    <span>case</span><span>1</span>: <span>echo</span><span>'误差:0-5s'</span>; <span>break</span>;    

    <span>case</span><span>2</span>: <span>echo</span><span>'误差: > 5s'</span>; <span>break</span>;  

    <span>default</span>: <span>echo</span><span>'硬件出错!'</span>; <span>break</span>;  
}  

<span>echo</span> gmttolocal(<span>$processdate</span>,<span>8</span>); <span>// 中国  </span><span><span>function</span><span>gmttolocal</span><span>(<span>$mydate</span>,<span>$mydifference</span>)</span>{</span><span>$datetime</span> = explode(<span>" "</span>,<span>$mydate</span>);  
    <span>$dateexplode</span> = explode(<span>"-"</span>,<span>$datetime</span>[<span>0</span>]);  
    <span>$timeexplode</span> = explode(<span>":"</span>,<span>$datetime</span>[<span>1</span>]);  
    <span>$unixdatetime</span> = mktime(<span>$timeexplode</span>[<span>0</span>]+<span>$mydifference</span>,<span>$timeexplode</span>[<span>1</span>],<span>0</span>,<span>$dateexplode</span>[<span>0</span>],<span>$dateexplode</span>[<span>1</span>],<span>$dateexplode</span>[<span>2</span>]);  
    <span>return</span> date(<span>"m/d/Y H:i:s"</span>,<span>$unixdatetime</span>);  
}  </code>
Copy after login

fsockopen explanation:

Open the Socket link of the network.
Syntax: int fsockopen(string hostname, int port, int [errno], string [errstr], int [timeout]);
Return value: integer
Function type: Network system

Content Description Currently, this function provides two Socket data flow interfaces, namely AF_INET for Internet and AF_UNIX for Unix. When used in the context of the Internet, the parameters hostname and port represent the URL and port number respectively. In the case of UNIX, you can do IPC, the hostname parameter indicates the path to the socket, and the port is configured as 0. The omitted timeout option indicates how long it will take before the connection is interrupted. After using this function, the file pointer will be returned for use by file functions, including fgets(), fgetss(), fputs(), fclose() and feof(). The parameters errno and errstr are also omitted and are mainly used for error handling. Using this function will use blocking mode processing, which can be converted to non-blocking mode with set_socket_blocking().

Author:leedaning
URL of this article: http://blog.csdn.net/leedaning/article/details/48547123

The above introduces the PHP acquisition time (system time and network time), including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template