IamMarvin1a Posted January 26, 2018 Share Posted January 26, 2018 (edited) 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 January 26, 2018 by IamMarvin1a Link to comment Share on other sites More sharing options...
IamMarvin1a Posted January 26, 2018 Author Share Posted January 26, 2018 (edited) 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 January 26, 2018 by IamMarvin1a Link to comment Share on other sites More sharing options...
justsomeguy Posted January 26, 2018 Share Posted January 26, 2018 That's the general idea, using PHP to print the current time value in Javascript. Note that this will be in the timezone that the server is set to. Link to comment Share on other sites More sharing options...
IamMarvin1a Posted January 30, 2018 Author Share Posted January 30, 2018 (edited) 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 January 30, 2018 by IamMarvin1a Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now