PHP implementation Countdown effect, _php tutorial

Source: Internet
Author: User
Tags php mysql

PHP to achieve the countdown effect,


Now a lot of group buying sites have the time remaining to display. Showing the time remaining can be implemented using JavaScript, but we will find it unsafe to use JavaScript because JavaScript gets the client's time. For example, the deal is over, but the technology-savvy visitor only has to change the time of his client computer to show that the item is still available for purchase. Obviously, this is not the original intention of our website design. When the group purchase is over, you can't buy it again. Here is a test system for the remaining time countdown to the display code and we discuss together.

Implementation principle:
PHP gets the server-side time, we just set the test start time and end time and the current time. If the current time is not within the time frame of our test, it is displayed to the examinee "not currently in the exam period!" ”。 If within the test time range, get the current time, the end time to lose the current time is the remaining time, the remaining time formatted output is the "remaining test time: 2 hours 56 minutes 32 seconds" in this form. After the server side has obtained the remaining time, we also have to dynamically display the countdown time remaining on the client. This needs to be implemented with Ajax. Before you begin, let's familiarize yourself with a few functions.

PHP Functions:
strtotime (); Convert any English date to a timestamp
Floor (); Rounding up the FA, and int () casting acquaintance
Json_encode () //JSON-encodes a variable, returns a string

Simple calculation of the remaining days:

Date_default_timezone_set (' Asia/hong_kong '); $startDate = ' 2015-8-11 '; $endDate = ' 2015-8-31 ';//convert date to UNIX timestamp $ Startdatestr = Strtotime ($startDate); $endtDateStr = Strtotime ($endDate); $total = $endtDateStr-$startDateStr; $now = Strtotime (Date (' y-m-d ')); $remain = $endtDateStr-$now; Echo ' Duration: '. $total/(3600*24). ' Days
'; Echo ' surplus: '. $remain/(3600*24). ' Days ';

Effect:

Simple calculation of remaining time:

Date_default_timezone_set (' Asia/hong_kong '); $startTime = ' 09:00:00 '; $endTime = ' 18:00:00 ';//convert Time to UNIX timestamp $ Starttimestr = Strtotime ($startTime); $endTimeStr = Strtotime ($endTime); $total = $endTimeStr-$startTimeStr; $restHours = 1; Rest 1 hours $now = strtotime (Date (' h:i:s ')), $remain = $endTimeStr-$now; Echo ' work time: '. ($total/3600-$restHours). ' Hours
' Echo ' also has: '. Floor (($remain/3600)). ' Hours '. Floor ($remain/60). ' Minutes off duty ';

Effect:

Front and back end mates to achieve exam remaining time:

HTML Layout

Exam remaining time: copy code code as follows: XX hours, minutes, seconds

JS Script

function DealData (id,value) {  var place = document.getElementById (ID);  place.innerhtml = value;} Window.setinterval (function () {//per second data is taken from the server  var ajax = new Ajax ();  Ajax.get ("remain_time.php?a=" +math.random (), function (data) {  eval ("var dtime =" +data);   DealData (' Hour ', dtime.hour);  DealData (' minute ', dtime.minute);  DealData (' second ', dtime.second); });},1000);

PHP Code:

Date_default_timezone_set (' PRC '); $start _time = ' 09:00:00 '; $end _time = ' 18:00:00 '; $start _famate_time = Strtotime ($ start_time);//start time is converted to timestamp $end_famate_time = Strtotime ($end _time); The end time is converted to timestamp $now_time = time (), if ($end _famate_time < $now _time | | $start _time > $now _time) {echo ' is not currently in the time range of the exam!} '; Exit;} $remain _time = $end _famate_time-$now _time; Number of seconds remaining $remain_hour = Floor ($remain _time/(60*60)); The remaining hours $remain_minute = Floor (($remain _time-$remain _hour*60*60)/60); Number of minutes remaining $remain_second = ($remain _time-$remain _hour*60*60-$remain _minute*60); Number of seconds left echo json_encode (' hour ' = ' = ' $remain _hour, ' minute ' = = $remain _minute, ' second ' = + $remain _second)) ;

The above is the PHP implementation of the key code countdown, I hope that everyone's learning has helped.

Articles you may be interested in:

    • Inserting the current time in PHP MySQL
    • PHP timestamp using instance code
    • PHP calculates the time interval between two timestamps (hours)
    • PHP set_time_limit (0) Set the program execution time function
    • PHP date to timestamp, specified date converted to timestamp
    • PHP+JS Implementation Countdown function
    • How to set the session expiration time accurately in PHP
    • How PHP gets the millisecond timestamp

http://www.bkjia.com/PHPjc/1084600.html www.bkjia.com true http://www.bkjia.com/PHPjc/1084600.html techarticle PHP to achieve the countdown effect, now a lot of group buying sites have the remaining time display. Showing the time remaining can be implemented using JavaScript, but we'll find that using JavaScript ...

  • 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.