PHP Products seconds Kill timer implementation code _php skills

Source: Internet
Author: User
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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.