PHP product flash sale timing implementation code_PHP tutorial

WBOY
Release: 2016-07-21 15:39:11
Original
967 people have browsed it

It is required to display a real-time countdown of hours, minutes and seconds. Modifying the date and time on the client side will not affect the normal display of the countdown (that is, based on the server time).

In fact, this is the same requirement as the time limit function of many exam and other systems.

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.

The problem now is to solve the impact of the user-side modified date and time 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 will be synchronized with the server time.

The theory is synchronization, but there will be an error of 1 second in actual testing. (The specific reason is related to the Internet speed. The faster the Internet speed, the smaller the error.) But this will never affect our above requirements.

Note: Flash sale time is from morning to 10pm.

Code is as follows:

Copy code The code is as follows:


//PHP time is calculated in seconds. js time is calculated in milliseconds

date_default_timezone_set('PRC');
//date_default_timezone_set("Asia/Hong_Kong");//Region

//Configure the daily activity time period
$starttimestr = "08:00:00";
$endtimestr = "22:00:00";

$starttime = strtotime($starttimestr);
$endtime = strtotime ($endtimestr);
$nowtime = time();
if ($nowtime<$starttime){
die("The event has not started yet, the event time is: {$starttimestr} to {$endtimestr }");
}
$lefttime = $endtime-$nowtime; //Actual remaining time (seconds)
?>

Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template