Php real-time countdown function implementation method details, php real-time countdown details
This article describes how to implement the real-time countdown function of php. We will share this with you for your reference. The details are as follows:
In the past few days, the company has to implement a time-limited shopping function. This requires a countdown and a real-time countdown.
Requirements:
1) display of real-time countdown in hours, minutes, And seconds
2) The modification date and time on the user end does not affect the normal display of the Countdown (that is, the server time prevails)
In fact, this is the same as the Time Limit Function of many examination systems.
Solution:
1) ajax cannot be used to retrieve the server time every second.
Therefore, real-time countdown must be implemented using javascript. This is very simple. Examples of online countdown.
2) now the problem is to solve the impact of modifying the date and time on our display.
The solution is to calculate the time difference between the client time and the server time.
In this way, you only need to run php once. The real-time countdown time is synchronized with the server time.
The theory is synchronous, but the actual test will have an error of 1 second. (the specific reason is that it is related to the network speed, and the faster the network speed, the smaller the error), but this will never affect our above requirements.
Instance:
Code:
<? Php // php time is calculated in seconds. The js time is calculated in milliseconds as date_default_timezone_set ("Asia/Hong_Kong"); // region // configure the daily activity period $ starttimestr = "09:00:00"; $ endtimestr = "18:30:00 "; $ starttime = strtotime ($ starttimestr); $ endtime = strtotime ($ endtimestr); $ nowtime = time ();?> <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
Instance 2:
Modified some of the above bugs.
Code:
<? Php // php time is calculated in seconds. The js time is calculated in milliseconds as date_default_timezone_set ("Asia/Hong_Kong"); // region // configure the daily activity period $ starttimestr = "09:00:00"; $ endtimestr = "18:30:00 "; $ starttime = strtotime ($ starttimestr); $ endtime = strtotime ($ endtimestr); $ nowtime = time ();?> <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
Instance 3:
Different ideas are much simpler.
Code:
<? Php // php time is calculated in seconds. The js time is calculated in milliseconds as date_default_timezone_set ("Asia/Hong_Kong"); // region // configure the daily activity period $ starttimestr = "09:00:00"; $ endtimestr = "13:50:00 "; $ starttime = strtotime ($ starttimestr); $ endtime = strtotime ($ endtimestr); $ nowtime = time (); if ($ nowtime <$ starttime) {die ("activity has not started yet. activity time: {$ starttimestr} to {$ endtimestr}") ;}$ lefttime = $ endtime-$ nowtime; // What is the actual remaining time (in seconds)?> <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
PS: here we recommend several time and date related tools for your reference:
Online date/day calculator:
Http://tools.jb51.net/jisuanqi/date_jisuanqi
Online date calculator/days calculator:
Http://tools.jb51.net/jisuanqi/datecalc
Online date-days difference calculator:
Http://tools.jb51.net/jisuanqi/onlinedatejsq
Unix timestamp Conversion Tool:
Http://tools.jb51.net/code/unixtime