Three ways to implement PHP real time countdown

Source: Internet
Author: User
Tags time in milliseconds time limit

Requirements:

1, to have the hour minutes seconds of real-time countdown display.

2, the user-side modification date time does not affect the countdown to the normal display (that is, the server time to prevail). In fact, this and many tests such as system time limit function requirements.

Solution Idea:

1, 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.

2, now the problem is to solve the user-side modification date time to our display impact. The solution is to compute the time difference between the user and the server, so that the problem is completely solved, only need to run a PHP, real-time countdown time and the time of the server synchronized. The theory is synchronized, but the actual test will have 1 seconds of error (the specific reason is the speed, the faster the speed, the smaller the error), but this will not affect the requirements of our above.

Code 1: <?php//php time is in seconds, JS time in milliseconds date_default_timezone_set ("Asia/hong_kong");//region//Configure daily activity time period $starttimestr = " 09:00:00 "; $endtimestr = "18:30:00"; $starttime = Strtotime ($STARTTIMESTR); $endtime = Strtotime ($ENDTIMESTR); $nowtime = time (); $lefttime = $endtime-$nowtime//Actual remaining time (sec)?>

Code 2, modified code 1 of some bugs: <?php//php time is in seconds, JS time in milliseconds date_default_timezone_set ("Asia/hong_kong");//region//Configure daily activity time period $ Starttimestr = "09:00:00"; $endtimestr = "18:30:00"; $starttime = Strtotime ($STARTTIMESTR); $endtime = Strtotime ($ENDTIMESTR); $nowtime = time (); $lefttime = $endtime-$nowtime//Actual remaining time (sec)?>

Code 3, the idea is different, simple much: <?php//php time is in seconds, JS time in milliseconds date_default_timezone_set ("Asia/hong_kong");//region//Configure daily activity time period $ Starttimestr = "09:00:00"; $endtimestr = "13:50: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)

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.