PHP获取某远程网站的服务器时间

时间:2023-11-24 14:58:20

<?php

function get_time($server){

$data  = "HEAD / HTTP/1.1\r\n";

$data .= "Host: $server\r\n";

$data .= "Connection: Close\r\n\r\n";

$fp = fsockopen($server, 80);

fputs($fp, $data);

$resp = '';

while ($fp && !feof($fp))

$resp .= fread($fp, 1024);

preg_match('/^Date: (.*)$/mi',$resp,$matches);

return strtotime($matches[1]);

}

?>

<?php

echo date('Y-m-d H:i:s',get_time("www.codefans.net"));

?>