Ver Mensaje Individual
  #4 (permalink)  
Antiguo 09/04/2013, 07:19
Avatar de emprear
emprear
Colaborador
 
Fecha de Ingreso: junio-2007
Ubicación: me mudé
Mensajes: 8.388
Antigüedad: 16 años, 10 meses
Puntos: 1567
Respuesta: servidor perdio la fecha y todo mal como prevenir?

Con php

Código PHP:
Ver original
  1. <?php header("Content-Type: text/html;charset=utf-8"); ?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  3. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4. <html xmlns="http://www.w3.org/1999/xhtml">
  5. <head>
  6. <title>Sincronizar tiempo timeServer + Php</title>
  7. <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  8. <style type="text/css">
  9. /*<![CDATA[*/
  10. body{
  11. font-family: verdana, sans-serif;
  12. font-size: 12pt;
  13. color:#241A17;
  14. }
  15. /*]]>*/
  16. </style>
  17. <script type="text/javascript">
  18. //<![CDATA[
  19. function HoraAMPM() {
  20. var fecha = new Date();
  21. var horas = fecha.getHours();
  22. var minutos = fecha.getMinutes();
  23. var ampm = horas >= 12 ? 'pm' : 'am';
  24. horas = horas % 12;
  25. horas = horas ? horas : 12;
  26. minutos = minutos < 10 ? '0'+minutos : minutos;
  27. var tiempo = horas + ':' + minutos + ' ' + ampm;
  28. document.write(tiempo);
  29. }
  30.  
  31. function Hora24() {
  32. var fecha = new Date();
  33. var horas = fecha.getHours();
  34. var minutos = fecha.getMinutes();
  35. minutos = minutos < 10 ? '0'+minutos : minutos;
  36. var tiempo = horas + ':' + minutos ;
  37. document.write(tiempo);
  38. }
  39.  
  40. //]]>
  41. </script>
  42. </head>
  43. <body>
  44. <?php
  45. function obtener_tiempo_tmeserver ($timeserver, $socket) {
  46.  
  47. $fp = fsockopen($timeserver,$socket,$err,$errstr,5);
  48. if ($fp) {
  49. fputs($fp,"\n");
  50. $valor_tiempo = fread($fp,49);
  51. fclose($fp);
  52. }else{
  53. $valor_tiempo = " ";
  54. }
  55.  
  56.   $ret = array();
  57.   $ret[] = $valor_tiempo;
  58.   $ret[] = $err;     # error code
  59.  $ret[] = $errstr;  # error text
  60.  return($ret);
  61.  
  62. } # function obtener_tiempo_tmeserver
  63.  
  64. $timeserver = "time.nist.gov"; // indicar el timeserver aqui
  65. $timercvd = obtener_tiempo_tmeserver($timeserver,37);
  66. if (!$timercvd[1]) {
  67.   $valor_tiempo = bin2hex ($timercvd[0]);
  68.   $valor_tiempo = abs (HexDec('7fffffff') - HexDec($valor_tiempo) - HexDec('7fffffff')) ;
  69.   $tmestamp = $valor_tiempo - 2208988800;
  70.   $datum = date("Y-m-d (D) H:i:s",$tmestamp - date("Z",$tmestamp));
  71.   $doy = (date("z",$tmestamp)+1);
  72.  
  73.   echo "Tiempo chequeado en el servidor <b>",$timeserver,"</b> : [<span style=\"color: red\">",$valor_tiempo,"</span>]";
  74.   echo " (segundos desde 1900-01-01 00:00.00).<br />\n";
  75.   echo "La fecha y hora actual es ",$datum," UTC.<br /> ";
  76.   echo "El valor unix de time es $tmestamp.<br />\n";
  77. }else{
  78.   echo "Error al intentar acceder al timeServer <b>$timeserver</b><p style=\"width: 500px;\">";
  79.   echo "Código de error: <b>" . $timercvd[1] . "</b><br /> " . htmlentities($timercvd[2]) . "<br /></p>\n";
  80. }
  81. ?>
  82. <div>
  83. <br />
  84. <p>Hora local con javascript</p>
  85. <script type="text/javascript">
  86. //<![CDATA[
  87. HoraAMPM();
  88. //]]>
  89. </script>
  90. <br />
  91. <script type="text/javascript">
  92. //<![CDATA[
  93. Hora24();
  94. //]]>
  95. </script>
  96. </div>
  97. </body>
  98. </html>

solo tenés que definir el TimeServer en la linea
$timeserver = "time.nist.gov"; // indicar el timeserver aqui

Saludos
__________________
La voz de las antenas va, sustituyendo a Dios.
Cuando finalice la mutación, nueva edad media habrá
S.R.