Now many of the group buying site has the remaining time display. Displaying the rest of the time can be implemented using JavaScript, but we'll find it unsafe to use JavaScript because JavaScript gets the client's time. For example, this is the end of the group, but the technology-literate visitor only changes the time of his client computer and shows that the product can also be purchased. Obviously, this is not the original intention of our website design. The end of the group purchase can not be bought again. Here is a test system for the rest of the time countdown to the display code and we discuss together.
Implementation principle:
PHP gets the server-side time, and we just set the start and end times and the current time of the test. If the current time is not within the scope of our test time, then show to the examinee "not currently in the exam period!" ”。 If within the test time range, get the current time, the end time minus the current time is the remaining time, the remaining time format output is "The remaining test time: 2 hours 56 minutes 32 seconds" form. After the server has been given the rest of the time, we also dynamically display the countdown to the rest of the time on the client side. This needs to be implemented with Ajax. Before you begin, you should be familiar with several functions.
PHP Functions:
strtotime (); Convert any English date to time stamp
floor (); The rounding method, and int () forced to convert the acquaintance
Json_encode () //JSON encodes a variable, returns a string
Simple remaining number of days calculation:
Date_default_timezone_set (' Asia/hong_kong ');
$startDate = ' 2015-8-11 ';
$endDate = ' 2015-8-31 ';
Convert date to unix timestamp
$startDateStr = Strtotime ($startDate);
$ENDTDATESTR = Strtotime ($endDate);
$total = $endtDateStr-$startDateStr;
$now = strtotime (Date (' y-m-d '));
$remain = $endtDateStr-$now;
Echo ' Duration: '. $total/(3600*24). ' Days <br> ';
Echo ' Left: '. $remain/(3600*24). ' Days ';
Effect:
Simple Residual time calculation:
Date_default_timezone_set (' Asia/hong_kong ');
$startTime = ' 09:00:00 ';
$endTime = ' 18:00:00 ';
Converts time to Unix timestamp
$startTimeStr = Strtotime ($startTime);
$ENDTIMESTR = Strtotime ($endTime);
$total = $endTimeStr-$startTimeStr;
$restHours = 1; Rest 1 hours
$now = strtotime (Date (' h:i:s '));
$remain = $endTimeStr-$now;
Echo ' Work time: '. ($total/3600-$restHours). ' Hours <br> ';
Echo ' and: '. Floor (($remain/3600)). ' Hours '. Floor ($remain/60). ' Minutes off work ';
Effect:
The front and rear side cooperate to realize the test remaining time:
HTML Layout
Test remaining time:
Copy Code code as follows:
<span id= "Hour" >00</span> hours <span id= "Minute" >00</span> min <span id= "Second" >00</ span> seconds
JS Script
function Dealdata (id,value) {
var place = document.getElementById (ID);
place.innerhtml = value;
}
Window.setinterval (function () {//Sec data is fetched from server
var ajax = new Ajax ();
Ajax.get ("remain_time.php?a=" +math.random (), function (data) {
eval ("var dtime =" +data);
Dealdata (' Hour ', dtime.hour);
Dealdata (' minute ', dtime.minute);
Dealdata (' second ', Dtime.second);
});
},1000);
PHP Code:
Date_default_timezone_set (' PRC ');
$start _time = ' 09:00:00 ';
$end _time = ' 18:00:00 ';
$start _famate_time = strtotime ($start _time);//start time converted to timestamp
$end _famate_time = strtotime ($end _time);//end time converted to timestamp
$now _time = time ();
if ($end _famate_time < $now _time | | $start _time > $now _time) {
echo ' is not currently in the test time range! ';
Exit;
}
$remain _time = $end _famate_time-$now _time; The remaining number of seconds
$remain _hour = Floor ($remain _time/(60*60));//The remaining hours
$remain _minute = Floor (($remain _time-$remain _ HOUR*60*60)/60); The remaining minutes
$remain _second = ($remain _time-$remain _hour*60*60-$remain _minute*60);//number of seconds left
Echo Json_encode ( Array (' Hour ' => $remain _hour, ' minute ' => $remain _minute, ' second ' => $remain _second));
The above is the PHP implementation of the countdown key code, I hope to help you learn.