Requirements:
1. There must be a real-time countdown display of hours, minutes and seconds.
2. Modifying the date and time on the client side will not affect the normal display of the countdown (that is, the server time shall prevail). In fact, this is the same as the requirements for system time limit functions such as many exams.
Solution:
1. You can’t use ajax to get the server time every second, so the real-time countdown must be implemented with javascript. This is very simple, there are a lot of examples on the Internet.
2. The problem now is to solve the impact of the date and time modified by the client on our display. The solution is to calculate the time difference between the client time and the server time, so that the problem is completely solved. You only need to run php once, and the real-time countdown time will be synchronized with the server time. The theory is synchronous, but the actual test will have an error of 1 second (the specific reason is related to the network speed. The faster the network speed, the smaller the error), but this will never affect our above requirements.
" 09:00:00"; $endtimestr = "18:30:00"; $starttime = strtotime($starttimestr); $endtime = strtotime($endtimestr); $nowtime = time(); $lefttime = $endtime-$ nowtime;//Actual remaining time (seconds)?>Code 2, modified some bugs in code 1:
Code 3, different ideas, much simpler: