Jump to content

Using PHP Server Date/TIme to Display Clock in a Website


IamMarvin1a

Recommended Posts

Hello. I already tried using JavaScript Clock With Timing Event Code like this:

<script>
function startTime() {
    var today = new Date();
    var h = today.getHours();
    var m = today.getMinutes();
    var s = today.getSeconds();
    m = checkTime(m);
    s = checkTime(s);
    document.getElementById('txt').innerHTML =
    h + ":" + m + ":" + s;
    var t = setTimeout(startTime, 500);
}
function checkTime(i) {
    if (i < 10) {i = "0" + i};  // add zero in front of numbers < 10
    return i;
}
</script>

But I notice it will be much better to use PHP Server Date/Time to avoid user's device incorrect time/clock.

Anyone here can help me to share code or other technique/solution on how to use PHP Server Date/Time to create clock with timing event to display in a website.

Edited by IamMarvin1a
Link to comment
Share on other sites

Hello. I got some code or other technique/solution which I found while researching in the internet. Here is the code:

 <div id="websiteClock"></div>
<script>
    var serverClock = new Date(<?php echo time() * 1000 ?>);
    function startInterval(){  
        setInterval('updateTime();', 1000);  
    }
    startInterval();//start the time interval
    function updateTime(){
        var currentMS = serverClock.getTime();
        currentMS += 1000;
        serverClock.setTime(currentMS);
        document.getElementById('websiteClock').innerHTML = serverClock.toDateString() + " " 
         + serverClock.toTimeString();
    } 
</script>

RIght now I am still looking for another way of solution/technique. Please share codes on this topic. Thank you.

Edited by IamMarvin1a
Link to comment
Share on other sites

I also have other codes using the setTimeout

<p id="HHootteeLLClock"></p>

<script>
var serverClock = new Date(<?php echo time() * 1000 ?>);
timeInterval(); //start the time interval
function timeInterval(){  
var currentMS = serverClock.getTime();
   currentMS += 1000;
   serverClock.setTime(currentMS);
   document.getElementById('HHootteeLLClock').innerHTML = "The date today is " + serverClock.toDateString() + "<br>" + "The time today is " +  serverClock.toTimeString();
   setTimeout(timeInterval, 1000);  
}
</script>

 

Edited by IamMarvin1a
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...