Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

200
Vistas
How to get server time with JS AND PHP?

I am creating a site where I get the time from the server in php and then pass it to a JS variable, this to perform some other actions.

This is my example of what I have:

Clock.php file: In this file I get the server time

<?php 

echo $s = date('Y-m-d h:i:s');

?>

Index.php file: In this file I get the result of the echo set in the file clock.php.

    <?php include("reloj.php"); ?>
    <input type="text" id="serverDate" style="opacity: 20;" >
<script>
    let reloj = document.getElementById("serverDate");
    
    function muestraReloj () {
            fetch("reloj.php")
            .then(response => response.text())
            .then(data => reloj.value = data); 
                console.log(reloj.value);
           var fechaYHora = new Date(reloj.value).toLocaleTimeString();
            console.log(fechaYHora);
    }
    setInterval(muestraReloj, 1000)
</script>

Everything works correctly for me, but I don't want to show the time that is printed in the echo of the clock file, in the example that I establish if I remove the echo then it fails because it does not show anything. The input with the id serverDate I can hide it but the echo of the file clock.php I don't know how it could be hidden, any ideas?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

replace this with your Clock.php file content:

<?php
    echo $_SERVER['REQUEST_TIME'];
?>
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda