PHP implementation of the real-time countdown function of a detailed approach

Source: Internet
Author: User
Tags time in milliseconds
This article mainly introduced the PHP real-time Countdown function realization method, combined with the concrete instance form analysis PHP with the foreground JS timestamp operation to realize the countdown function specific steps and the related operation skill, needs the friend can refer to the next

In this paper, the implementation of PHP real-time countdown function is described. Share to everyone for your reference, as follows:

These days the company has to do a time-limited shopping function. This is going to be the countdown, there must be a countdown.

Requirements:

1) The display of the real time countdown to the hour minute seconds
2) User-side modification date time does not affect the normal display of the countdown (that is, the server time)

In fact, this and a lot of examinations and other systems of time limit function the same requirements.

Solution Ideas:

1) It is not always possible to get server time with Ajax every second.

So the real-time countdown must be implemented in JavaScript. This is simple. A bunch of examples online.

2) The problem now is to resolve the effect of the user-side modification date and time on our display.
The solution is to calculate the time difference between the client and the server. The problem has been solved.

This only requires PHP to run once. The time of the real-time countdown is synchronized with the server's time.

The theory is synchronous, 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 not affect our requirements above.

Instance:

Code:

The time of <?php//php is counted in seconds. JS time in milliseconds to calculate Date_default_timezone_set ("Asia/hong_kong");//region//Configure the active time period for each day $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 ">

Example 2:

Modified some of the above bugs

Code:

The time of <?php//php is counted in seconds. JS time in milliseconds to calculate Date_default_timezone_set ("Asia/hong_kong");//region//Configure the active time period for each day $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 ">

Example 3:

Different ideas, much simpler.

Code:

The time of <?php//php is counted in seconds. JS time in milliseconds to calculate Date_default_timezone_set ("Asia/hong_kong");//region//Configure the active time period for each day $starttimestr = "09:00:00"; $endtimestr = " 13:50:00 "; $starttime = Strtotime ($starttimestr); $endtime = Strtotime ($endtimestr); $nowtime = time (); if ($nowtime <$ StartTime) {die ("activity has not started, the active time is: {$starttimestr} to {$endtimestr}");} $lefttime = $endtime-$nowtime; The actual time remaining (seconds)? ><! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

The above is the whole content of this article, I hope that everyone's study has helped.


Related Article

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.