How to achieve network time synchronization communication through PHP and NTP protocol
Overview:
Network Time Protocol (NTP for short) is a protocol used to synchronize computer system time. In network applications, accurate time synchronization is very important to ensure the normal operation of network services. In PHP, network time synchronization can be achieved by communicating with the NTP protocol. This article will introduce how to use PHP code to communicate with an NTP server to obtain accurate network time.
Steps:
$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); $ntpServer = "time.google.com"; $ntpPort = 123; $timeout = 5; socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => $timeout, 'usec' => 0)); socket_connect($socket, $ntpServer, $ntpPort);
Here, we use the socket_create function to create a UDP socket and establish Connection to NTP server. The $ntpServer variable stores the address of the NTP server, the $ntpPort variable specifies the NTP port number (default is 123), and the $timeout variable defines the timeout for establishing a connection with the NTP server.
$request = "".str_repeat("", 47); // NTP请求包 socket_send($socket, $request, strlen($request), 0);
In the NTP request, the first byte is the protocol version and mode. ' ' indicates that the client wants to get the server's time. The next 47 bytes are set to 0.
if (socket_recv($socket, $response, 48, MSG_WAITALL) === FALSE) { // 处理接收超时的情况 die("无法接收到响应包"); }
Use the socket_recv function to receive NTP response packet. The MSG_WAITALL parameter guarantees that 48 bytes of data are received at one time. If the reception times out, FALSE will be returned.
$unpacked = unpack("N12", $response); $timestamp = ($unpacked[9] - 2208988800); // 将NTP时间戳转换为Unix时间戳 $time = date("Y-m-d H:i:s", $timestamp); // 格式化时间
Use the unpack function to parse the 48-byte binary data into 12 32-bit integers. The NTP timestamp is a 32-bit integer starting from January 1, 1900, while the Unix timestamp is a 32-bit integer starting from January 1, 1970. You can convert the NTP timestamp to a Unix timestamp by subtracting 2208988800. Finally, use the date function to format the timestamp into a readable time.
socket_close($socket);
After completing the NTP time synchronization, use the socket_close function to close the connection with the NTP server.
Sample code:
$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); $ntpServer = "time.google.com"; $ntpPort = 123; $timeout = 5; socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => $timeout, 'usec' => 0)); socket_connect($socket, $ntpServer, $ntpPort); $request = "".str_repeat("", 47); // NTP请求包 socket_send($socket, $request, strlen($request), 0); if (socket_recv($socket, $response, 48, MSG_WAITALL) === FALSE) { // 处理接收超时的情况 die("无法接收到响应包"); } $unpacked = unpack("N12", $response); $timestamp = ($unpacked[9] - 2208988800); // 将NTP时间戳转换为Unix时间戳 $time = date("Y-m-d H:i:s", $timestamp); // 格式化时间 socket_close($socket); echo "同步时间为:".$time;
The above sample code achieves network time synchronization through PHP and NTP protocols. You can embed the above code into your PHP application to ensure the time accuracy of the network service.
The above is the detailed content of How to achieve network time synchronization communication through PHP and NTP protocol. For more information, please follow other related articles on the PHP Chinese website!