{"id":128,"date":"2011-09-25T08:08:28","date_gmt":"2011-09-25T08:08:28","guid":{"rendered":"http:\/\/mitat.tuu.fi\/?p=128"},"modified":"2014-02-08T09:49:42","modified_gmt":"2014-02-08T09:49:42","slug":"great-way-to-keep-your-server-in-correct-time-with-php","status":"publish","type":"post","link":"http:\/\/mitat.tuu.fi\/?p=128","title":{"rendered":"Great way to keep your server in correct time with PHP"},"content":{"rendered":"<p>This really works. From here: http:\/\/www.xenocafe.com\/tutorials\/php\/ntp_time_synchronization\/index.php<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n#!\/usr\/bin\/php -q\r\n&lt;?php\r\n  \/\/ ntp time servers to contact\r\n  \/\/ we try them one at a time if the previous failed (failover)\r\n  \/\/ if all fail then wait till tomorrow\r\n  $time_servers = array(&quot;time.nist.gov&quot;,\r\n                        &quot;nist1.datum.com&quot;,\r\n                        &quot;time-a.timefreq.bldrdoc.gov&quot;,\r\n                        &quot;utcnist.colorado.edu&quot;);\r\n\r\n  \/\/ date and clock programs (change for your system)\r\n  $date_app  = &quot;\/bin\/date&quot;;\r\n  $clock_app = &quot;\/sbin\/clock&quot;;\r\n\r\n  \/\/ a flag and number of servers\r\n  $valid_response = false;\r\n  $ts_count = sizeof($time_servers);\r\n\r\n  \/\/ time adjustment\r\n  \/\/ I'm in California and the clock will be set to -0800 UTC [8 hours] for PST\r\n  \/\/ you will need to change this value for your region (seconds)\r\n  $time_adjustment = 0;\r\n\r\n  for ($i=0; $i&lt;$ts_count; $i++) {\r\n    $time_server = $time_servers[$i];\r\n    $fp = fsockopen($time_server, 37, $errno, $errstr, 30);\r\n    if (!$fp) {\r\n      echo &quot;$time_server: $errstr ($errno)\\n&quot;;\r\n      echo &quot;Trying next available server...\\n\\n&quot;;\r\n    } else {\r\n      $data = NULL;\r\n      while (!feof($fp)) {\r\n        $data .= fgets($fp, 128);\r\n      }\r\n      fclose($fp);\r\n\r\n      \/\/ we have a response...is it valid? (4 char string -&gt; 32 bits)\r\n      if (strlen($data) != 4) {\r\n        echo &quot;NTP Server {$time_server} returned an invalid response.\\n&quot;;\r\n        if ($i != ($ts_count - 1)) {\r\n          echo &quot;Trying next available server...\\n\\n&quot;;\r\n        } else {\r\n          echo &quot;Time server list exhausted\\n&quot;;\r\n        }\r\n      } else {\r\n        $valid_response = true;\r\n        break;\r\n      }\r\n    }\r\n  }\r\n\r\n  if ($valid_response) {\r\n    \/\/ time server response is a string - convert to numeric\r\n    $NTPtime = ord($data{0})*pow(256, 3) + ord($data{1})*pow(256, 2) + ord($data{2})*256 + ord($data{3});\r\n    \/\/ convert the seconds to the present date &amp; time\r\n    \/\/ 2840140800 = Thu, 1 Jan 2060 00:00:00 UTC\r\n    \/\/ 631152000  = Mon, 1 Jan 1990 00:00:00 UTC\r\n    $TimeFrom1990 = $NTPtime - 2840140800;\r\n    $TimeNow = $TimeFrom1990 + 631152000;\r\n    \/\/ set the system time\r\n    $TheDate = date(&quot;m\/d\/Y H:i:s&quot;, $TimeNow + $time_adjustment);\r\n    $success = exec(&quot;$date_app -s \\&quot;$TheDate\\&quot;&quot;);\r\n    \/\/ set the hardware clock (optional) - you may want to comment this out\r\n    exec(&quot;$clock_app --systohc&quot;);\r\n    echo &quot;The server's date and time was set to $success\\n&quot;;\r\n  } else {\r\n    echo &quot;The system time could not be updated. No time servers available.\\n&quot;;\r\n  }\r\n?&gt;\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This really works. From here: http:\/\/www.xenocafe.com\/tutorials\/php\/ntp_time_synchronization\/index.php<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-128","post","type-post","status-publish","format-standard","hentry","category-code"],"_links":{"self":[{"href":"http:\/\/mitat.tuu.fi\/index.php?rest_route=\/wp\/v2\/posts\/128","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/mitat.tuu.fi\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/mitat.tuu.fi\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/mitat.tuu.fi\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/mitat.tuu.fi\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=128"}],"version-history":[{"count":5,"href":"http:\/\/mitat.tuu.fi\/index.php?rest_route=\/wp\/v2\/posts\/128\/revisions"}],"predecessor-version":[{"id":247,"href":"http:\/\/mitat.tuu.fi\/index.php?rest_route=\/wp\/v2\/posts\/128\/revisions\/247"}],"wp:attachment":[{"href":"http:\/\/mitat.tuu.fi\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=128"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/mitat.tuu.fi\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=128"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/mitat.tuu.fi\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=128"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}