Ver Mensaje Individual
  #1 (permalink)  
Antiguo 02/11/2012, 19:10
fcosun
 
Fecha de Ingreso: octubre-2011
Mensajes: 153
Antigüedad: 12 años, 6 meses
Puntos: 7
Ajax - obtener la hora del servidor dinamicamente

Como puedo obtener la hora del servidor de forma dinamica, tal como lo hace en javascript pero del servidor aca tengo algo de codigo:

Código Javascript:
Ver original
  1. <html>
  2. <head>
  3. <title>Reloj con Javascript</title>
  4. <script language="JavaScript">
  5. function mueveReloj(){
  6. momentoActual = new Date()
  7. hora = momentoActual.getHours()
  8. minuto = momentoActual.getMinutes()
  9. segundo = momentoActual.getSeconds()
  10.  
  11. horaImprimible = hora + " : " + minuto + " : " + segundo
  12.  
  13. document.form_reloj.reloj.value = horaImprimible
  14.  
  15. setTimeout("mueveReloj()",1000)
  16. }
  17. </script>
  18. </head>
  19.  
  20. <body onload="mueveReloj()">
  21.  
  22. Vemos aquí el reloj funcionando...
  23.  
  24. <form name="form_reloj">
  25. <input type="text" name="reloj" size="10">
  26. </form>
  27.  
  28. </body>
  29. </html>


Código HTML:
Ver original
  1. <!DOCTYPE html>
  2. function HoraAjax()
  3. {
  4. var xmlhttp;
  5. if (window.XMLHttpRequest)
  6.   {// code for IE7+, Firefox, Chrome, Opera, Safari
  7.   xmlhttp=new XMLHttpRequest();
  8.   }
  9. else
  10.   {// code for IE6, IE5
  11.   xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  12.   }
  13. xmlhttp.onreadystatechange=function()
  14.   {
  15.   if (xmlhttp.readyState==4 && xmlhttp.status==200)
  16.    {
  17.    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
  18.     }
  19.   }
  20. xmlhttp.open("GET","ObtenerHora.php",true);
  21. xmlhttp.send();
  22. }
  23. </head>
  24.  
  25. <div id="myDiv"><h2>Prueba de Ajax</h2></div>
  26. <button type="button" onclick="HoraAjax()">Contenido</button>
  27.  
  28. </body>
  29. </html>

ObtenerHora

Código PHP:
Ver original
  1. <?php
  2. echo date("H:i:s");
  3. ?>
__________________
Mi mail: [email protected]

Última edición por fcosun; 02/11/2012 a las 19:16