PHP product second-kill timing implementation code _ PHP Tutorial

Source: Internet
Author: User
PHP product second kill timing implementation code. A real-time countdown display of hours, minutes, and seconds is required. the modification date and time on the user side does not affect the normal display of the countdown (that is, the server time prevails ). In fact, this and many requirements require a real-time countdown display of hours, seconds, the user side to modify the date and time does not affect the normal display of the countdown (that is, based on the server time ).

In fact, this is the same as the time limit function of many examination systems.

You cannot use ajax to retrieve server time every second, so real-time countdown must be implemented using javascript. This is a simple example.

The problem 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 solved.

In this way, you only need to run php once, and the real-time countdown time is synchronized with the server.

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.

Note: The second kill time ranges from PM to PM.

The Code is as follows:

The code is as follows:



// The php time is calculated in seconds. The js time is measured in milliseconds.

Date_default_timezone_set ('prc ');
// Date_default_timezone_set ("Asia/Hong_Kong"); // region

// Configure the daily activity period
$ Starttimestr = "08:00:00 ";
$ Endtimestr = "22:00:00 ";

$ Starttime = strtotime ($ starttimestr );
$ Endtime = strtotime ($ endtimestr );
$ Nowtime = time ();
If ($ nowtime <$ starttime ){
Die ("activity hasn't started yet. activity Time: {$ starttimestr} to {$ endtimestr }");
}
$ Lefttime = $ endtime-$ nowtime; // the actual remaining time (seconds)
?>

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.