javascript - 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?
Answer
Solution:
replace this with your Clock.php file content:
<?php
echo $_SERVER['REQUEST_TIME'];
?>
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: the requested url was not found on this server. xampp
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.