在PHP中从Internet获取日期和时间[重复]

This question already has an answer here:

I know this type of related question is asked many times here. But I didn't found solution for what I am actually searching for.

So here is my question, How to get date from internet(not from local computer)?.

In my project I have a mail notification module. so I need proper date, even if nodes(local computer) date and time is changed. The program has to compare with the correct date. so I need to know how to fetch a date from internet or give me any other solution to get correct date and time even if the computer date and time is changed.

</div>

Call a free API that is available on the internet and get the time.

The easiest I found was http://www.convert-unix-time.com/api. You can obtain the current timestamp for Vienna by http://www.convert-unix-time.com/api?timestamp=now&timezone=vienna .

They also have PHP examples too.

$timestamp = time();
$returnType = 'php';
$timezone = 'Vienna';
$requestUri = sprintf('http://www.convert-unix-time.com/api?timestamp=%s&timezone=%s&returnType=%s',
    $timestamp, $timezone, $returnType);

$response = file_get_contents($requestUri);
$result = unserialize($response);
var_dump($result);

Another example API would be http://www.geonames.org/export/web-services.html#timezone .

A call to http://api.geonames.org/timezone?lat=47.01&lng=10.2&username=demo would return the following.

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<geonames>
    <timezone tzversion="tzdata2017c">
    <countryCode>AT</countryCode>
    <countryName>Austria</countryName>
    <lat>47.01</lat>
    <lng>10.2</lng>
    <timezoneId>Europe/Vienna</timezoneId>
    <dstOffset>2.0</dstOffset>
    <gmtOffset>1.0</gmtOffset>
    <rawOffset>1.0</rawOffset>
    <time>2018-01-02 06:57</time>
<sunrise>2018-01-02 08:05</sunrise>
<sunset>2018-01-02 16:41</sunset>
</timezone>
</geonames>

If you want to get time default time zone than use that way

echo $date = date('m/d/Y h:i:s a', time());

If you want to set your time zone than use "date_default_timezone_set('your_time_zone');"

date_default_timezone_set('Australia/Melbourne');
echo $date = date('m/d/Y h:i:s a', time());

List TimeZone

date_default_timezone_set(" **PLACE NEEDED TIMEZONE HERE**");

/* Query a time server (C) 1999-09-29, Ralf D. Kloth (QRQ.software) <ralf at     qrq.de> */
function query_time_server ($timeserver, $socket)
{
    $fp = fsockopen($timeserver,$socket,$err,$errstr,5);
    # parameters: server, socket, error code, error text, timeout
    if($fp)
    {
        fputs($fp, "
");
        $timevalue = fread($fp, 49);
        fclose($fp); # close the connection
    }
    else
    {
        $timevalue = " ";
    }

    $ret = array();
    $ret[] = $timevalue;
    $ret[] = $err;     # error code
    $ret[] = $errstr;  # error text
    return($ret);
}

function getCurrentDate($format = "d/m/Y H:i:s"){
    $timeserver = "ntp.pads.ufrj.br";
    $timercvd = query_time_server($timeserver, 37);
//if no error from query_time_server
    if(!$timercvd[1]) {
        $timevalue = bin2hex($timercvd[0]);
        $timevalue = abs(HexDec('7fffffff') - HexDec($timevalue) - HexDec('7fffffff'));
        $tmestamp = $timevalue - 2208988800; # convert to UNIX epoch time stamp
        return date($format, $tmestamp);
    }
    return null;
}

echo getCurrentDate();