Requirements:
1, to have the hour minutes seconds of real-time countdown display.
2, the user-side modification date time does not affect the countdown to the normal display (that is, the server time to prevail). In fact, this and many tests such as system time limit function requirements.
Solution Idea:
1, 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.
2, now the problem is to solve the user-side modification date time to our display impact. The solution is to compute the time difference between the user and the server, so that the problem is completely solved, only need to run a PHP, real-time countdown time and the time of the server synchronized. The theory is synchronized, but the actual test will have 1 seconds of error (the specific reason is the speed, the faster the speed, the smaller the error), but this will not affect the requirements of our above.
Code 1: <?php//php time is in seconds, JS time in milliseconds date_default_timezone_set ("Asia/hong_kong");//region//Configure daily activity time period $starttimestr = " 09:00:00 "; $endtimestr = "18:30:00"; $starttime = Strtotime ($STARTTIMESTR); $endtime = Strtotime ($ENDTIMESTR); $nowtime = time (); $lefttime = $endtime-$nowtime//Actual remaining time (sec)?>
Code 2, modified code 1 of some bugs: <?php//php time is in seconds, JS time in milliseconds date_default_timezone_set ("Asia/hong_kong");//region//Configure daily activity time period $ Starttimestr = "09:00:00"; $endtimestr = "18:30:00"; $starttime = Strtotime ($STARTTIMESTR); $endtime = Strtotime ($ENDTIMESTR); $nowtime = time (); $lefttime = $endtime-$nowtime//Actual remaining time (sec)?>
Code 3, the idea is different, simple much: <?php//php time is in seconds, JS time in milliseconds date_default_timezone_set ("Asia/hong_kong");//region//Configure daily activity time period $ Starttimestr = "09:00:00"; $endtimestr = "13:50: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)