Required to have a real-time countdown to the hour minutes of the display, the client modified date time does not affect the countdown to the normal display (that is, the server time).
In fact, this and a lot of exam systems such as the time limit function the same requirements.
Always can not use Ajax every second to get server time, so real-time countdown must be implemented with JavaScript. This is simple, a bunch of examples on the web.
The problem now is to fix the effect of the client modification date time on our display.
The solution is to compute the time difference between the client and the server, so the problem is solved.
This only needs to run once PHP, the real-time countdown time and the server time synchronization.
The theory is synchronized, but the actual test will have a 1-second error. (The specific reason is connected with the speed, the faster the speed, the smaller the error), but this will never affect the requirements of our above.
Note: The seconds kill time from the morning point to 10 o'clock in the evening.
The Code is as follows:
Copy Code code as follows:
<?php
PHP's time is in seconds. JS time in milliseconds
Date_default_timezone_set (' PRC ');
Date_default_timezone_set ("Asia/hong_kong");//region
Configure the daily active time period
$starttimestr = "08:00:00";
$endtimestr = "22:00:00";
$starttime = Strtotime ($STARTTIMESTR);
$endtime = Strtotime ($ENDTIMESTR);
$nowtime = time ();
if ($nowtime < $starttime) {
Die ("activity has not yet started, activity time is: {$starttimestr} to {$endtimestr}");
}
$lefttime = $endtime-$nowtime; Actual remaining time (in seconds)
?>
<script language= "JavaScript" >
<!--//
var runtimes = 0;
function Getrtime () {
var NMS = <?= $lefttime?>*1000-runtimes*1000;
var Nh=math.floor (nms/(1000*60*60))%24;
var Nm=math.floor (nms/(1000*60))% 60;
var ns=math.floor (nms/1000)% 60;
document.getElementById ("Remainh"). Innerhtml=nh;
document.getElementById ("REMAINM"). innerhtml=nm;
document.getElementById ("remains"). Innerhtml=ns;
if (nms>5*59*1000&&nms<=5*60*1000)
{
Alert ("Last five minutes!") ");
}
runtimes++;
SetTimeout ("Getrtime ()", 1000);
}
Window.onload=getrtime;
-->
</script>