{"id":57,"date":"2015-10-12T06:42:52","date_gmt":"2015-10-12T04:42:52","guid":{"rendered":"http:\/\/lab.fawno.com\/?p=57"},"modified":"2022-03-07T01:04:07","modified_gmt":"2022-03-07T00:04:07","slug":"","status":"publish","type":"post","link":"https:\/\/lab.fawno.com\/en\/2015\/10\/12\/ntp-consultar-la-hora\/","title":{"rendered":"","raw":""},"content":{"rendered":"","protected":false,"raw":""},"excerpt":{"rendered":"","protected":false,"raw":""},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","_editorskit_title_hidden":false,"_editorskit_reading_time":0,"_editorskit_typography_data":[],"_editorskit_blocks_typography":"","_editorskit_is_block_options_detached":false,"_editorskit_block_options_position":"{}","_es_post_content":"\n

El protocolo NTP<\/a> provee de un servicio de hora a trav\u00e9s de Internet. Resumiendo mucho se trata de poder consultar la hora desde los relojes at\u00f3micos dispersos por el mundo y que marcan la hora exacta para todo el mundo.<\/p>\n\n\n\n

Cualquier ordenador puede actuar como servidor de hora para otros ordenadores, y se disponen de servidores de hora p\u00fablicos que dan un servicio muy confiable. En ntp.org<\/a> podemos encontrar toda la informaci\u00f3n.<\/p>\n\n\n\n

Para poder consultar la hora mediante NTP en PHP no existe ninguna librer\u00eda, por lo que hay que recurrir a clases o funciones. Mirando por aqu\u00ed y por all\u00e1 lo que he visto no funcionaba, as\u00ed que he creado la siguiente funci\u00f3n:<\/p>\n\n\n\n

function ntp_time ($host) {\n  $msg = hex2bin('e30004fa000100000001000000000000000000000000000000000000000000000000000000000000');\n  $data = false;\n  if ($sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP)) {\n    if (@socket_connect($sock, $host, 123)) {\n      list($xmt['time2'], $xmt['time1']) = explode(' ', microtime());\n      $xmt['time1'] += 2208988800;\n      $xmt['time2'] *= 4294967296;\n      $msg .= pack('N*', $xmt['time1'], $xmt['time2']);\n      if (@socket_write($sock, $msg)) {\n        if ($recv = @socket_read($sock, 48)) {\n          $data = unpack('H2ntp.flags\/C1ntp.stratum\/C1ntp.ppoll\/c1ntp.precision\/N1ntp.rootdelay\/N1ntp.rootdispersion\/H8ntp.refid\/N2ntp.reftime\/N2ntp.org\/N2ntp.rec\/N2ntp.xmt', $recv);\n          $data['hex'] = bin2hex($recv);\n          $data['ntp.rootdelay'] = $data['ntp.rootdelay'] \/ pow(2, 16);\n          $data['ntp.rootdispersion'] = $data['ntp.rootdispersion'] \/ pow(2, 16);\n          $data['ntp.refid'] = implode('.', unpack('C*', hex2bin($data['ntp.refid'])));\n          $data['ntp.reftime1'] = sprintf('%u', $data['ntp.reftime1']) - 2208988800;\n          $data['ntp.reftime2'] = floor(1000000 * (sprintf('%u', $data['ntp.reftime2']) \/ pow(2, 32))) \/ 1000000;\n          $data['ntp.org1'] = sprintf('%u', $data['ntp.org1']) - 2208988800;\n          $data['ntp.org2'] = floor(1000000 * (sprintf('%u', $data['ntp.org2']) \/ pow(2, 32))) \/ 1000000;\n          $data['ntp.rec1'] = sprintf('%u', $data['ntp.rec1']) - 2208988800;\n          $data['ntp.rec2'] = floor(1000000 * (sprintf('%u', $data['ntp.rec2']) \/ pow(2, 32))) \/ 1000000;\n          $data['ntp.xmt1'] = sprintf('%u', $data['ntp.xmt1']) - 2208988800;\n          $data['ntp.xmt2'] = floor(1000000 * (sprintf('%u', $data['ntp.xmt2']) \/ pow(2, 32))) \/ 1000000;\n        }\n      }\n    }\n  }\n\n  if (!empty($sock)) socket_close($sock);\n\n  if ($data === false) {\n    $err_nbr = socket_last_error();\n    if (!empty($sock)) $err_nbr = socket_last_error($sock);\n    $err_msg = socket_strerror($err_nbr);\n    echo $err_nbr, ' - ', $err_msg, \"n\";\n  }\n\n  return $data;\n}<\/pre>\n","_es_post_name":"ntp-consultar-la-hora","_es_post_excerpt":"","_es_post_title":"NTP: Consultar la hora","_en_post_content":"","_en_post_name":"","_en_post_excerpt":"","_en_post_title":"","edit_language":"en","footnotes":""},"categories":[5,3],"tags":[16,6],"class_list":["post-57","post","type-post","status-publish","format-standard","hentry","category-php","category-tips","tag-ntp","tag-php"],"ninja_gutenberg_blocks_featured_media_urls":{"thumbnail":"","ninja_gutenberg_blocks_landscape_large":"","ninja_gutenberg_blocks_portrait_large":"","ninja_gutenberg_blocks_square_large":"","ninja_gutenberg_blocks_landscape":"","ninja_gutenberg_blocks_portrait":"","ninja_gutenberg_blocks_square":"","full":""},"_links":{"self":[{"href":"https:\/\/lab.fawno.com\/en\/wp-json\/wp\/v2\/posts\/57","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lab.fawno.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lab.fawno.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lab.fawno.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lab.fawno.com\/en\/wp-json\/wp\/v2\/comments?post=57"}],"version-history":[{"count":26,"href":"https:\/\/lab.fawno.com\/en\/wp-json\/wp\/v2\/posts\/57\/revisions"}],"predecessor-version":[{"id":1848,"href":"https:\/\/lab.fawno.com\/en\/wp-json\/wp\/v2\/posts\/57\/revisions\/1848"}],"wp:attachment":[{"href":"https:\/\/lab.fawno.com\/en\/wp-json\/wp\/v2\/media?parent=57"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lab.fawno.com\/en\/wp-json\/wp\/v2\/categories?post=57"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lab.fawno.com\/en\/wp-json\/wp\/v2\/tags?post=57"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}