1

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.

Ishan Thilina Somasiri
  • 1,179
  • 1
  • 12
  • 24
Avinash T
  • 55
  • 3
  • 9

3 Answers3

2

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

Nims Patel
  • 1,048
  • 9
  • 19
  • 1
    This is a local timezone. The question says 'How to get date from internet(not from local computer)?.' – Dexter Jul 01 '19 at 02:28
1

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>
Ishan Thilina Somasiri
  • 1,179
  • 1
  • 12
  • 24
  • Hi @Ishan, How set the country code and name. and how to fetch the details from this xml output to php program. – Avinash T Jan 02 '18 at 06:12
  • @AvinashT Check the updated answer. – Ishan Thilina Somasiri Jan 02 '18 at 08:43
  • 1
    don't bother yourself with 'hacks'. Just configure your server so it'll know the correct time, set the correct timezone and be done with it. There are all sorts of funky datetime functions in php built in if you need times in other zones then the one you're in. Besides, querying an api takes time, so you'll never get the correct time.. – giorgio Jan 02 '18 at 09:27
0
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, "\n");
        $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();
ramin ashrafimanesh
  • 1,002
  • 10
  • 13