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?
replace this with your Clock.php file content:
<?php
echo $_SERVER['REQUEST_TIME'];
?>