When using php, you may often use standard time. There are two methods.
The first method:
Get the server time
First configuredate.timezone = PRC in php.ini
or adddate_default_timezone_set("PRC");
directly on the program page. Then you can usetime ()
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.
$fp=fsockopen('time.nist.gov',13,$errno,$errstr,90); echo fread($fp,2010);
The data obtained is similar to this:
55545 10-12-15 21:40:47 00 0 0 113.1UTC(NIST) *
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
$fp=fsockopen('time.nist.gov',13,$errno,$errstr,90); $ufc = explode(' ',fread($fp,date('Y'))); $date = explode('-',$ufc[1]); $processdate = $date[1].'-'.$date[2].'-'. date('Y').' '.$ufc[2]; switch($ufc[5]) { case0: echo'精确'; break; case1: echo'误差:0-5s'; break; case2: echo'误差: > 5s'; break; default: echo'硬件出错!'; break; } echo gmttolocal($processdate,8); // 中国 functiongmttolocal($mydate,$mydifference){$datetime = explode(" ",$mydate); $dateexplode = explode("-",$datetime[0]); $timeexplode = explode(":",$datetime[1]); $unixdatetime = mktime($timeexplode[0]+$mydifference,$timeexplode[1],0,$dateexplode[0],$dateexplode[1],$dateexplode[2]); return date("m/d/Y H:i:s",$unixdatetime); }
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 doIPC, 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.