Php implements countdown. _ PHP Tutorial

Source: Internet
Author: User
Tags php mysql
Php implements countdown ,. Php implements the countdown effect. Currently, many group buying websites have time remaining to display the results. The remaining time can be implemented using Javascript, but we will find that using Javascript to implement the countdown effect of php,

Currently, many group buying websites have time remaining. Javascript can be used to display the remaining time, but we will find that Javascript is not safe because Javascript obtains the client time. For example, this group purchase is over, but a technical visitor only needs to modify the time of his client computer to display that the product can be purchased. Obviously, this is not the original intention of our website design. After group buying is over, you cannot purchase more. Here we have written a display code for the remaining time countdown of the examination system to discuss with you.

Implementation principle:
PHP obtains the server-side time. we only need to set the test start time, end time, and current time. If the current time is not within our exam time range, it is displayed to the examinee "not in the exam time range !". If it is within the exam time range, the current time is obtained. the end time minus the current time is the remaining time, and the remaining time is formatted and output as "remaining exam time: 2 hours, 56 minutes, 32 seconds. After the server obtains the remaining time, we need to dynamically display the remaining time countdown on the client. This requires AJAX. Before getting started, you should familiarize yourself with several functions!

PHP functions:
Strtotime ();// Convert any English date into a timestamp
Floor ();// The rounding method is used to get an integer, which is used to forcibly convert int ().
Json_encode ()// Encode the variable in JSON format and return the string

Simple remaining days calculation:

Date_default_timezone_set ('Asia/Hong_Kong '); $ startDate = '2017-8-11'; $ endDate = '2017-8-31 '; // Convert the date to the 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 ). 'Day'
'; Echo 'remaining:'. $ remain/(3600*24). 'day ';

Effect:

Simple remaining time calculation:

Date_default_timezone_set ('Asia/Hong_Kong '); $ startTime = '09: 00: 00'; $ endTime = '18: 00: 00 '; // Convert the time to unix timestamp $ startTimeStr = strtotime ($ startTime); $ endTimeStr = strtotime ($ endTime); $ total = $ endTimeStr-$ startTimeStr; $ restHours = 1; // 1 hour of rest $ now = strtotime (date ('H: I: s'); $ remain = $ endTimeStr-$ now; echo 'start Time :'. ($ total/3600-$ restHours ). 'hour
'; Echo' and: '. floor ($ remain/3600). 'Hourly'. floor ($ remain/60). 'minute off work ';

Effect:

The remaining time of the examination is achieved through the combination of the front end and back end:

HTML layout

Remaining exam time: The code is as follows: 00 hour 00 minute 00 seconds

JS script

Function dealData (id, value) {var place = document. getElementById (id); place. innerHTML = value;} window. setInterval (function () {// retrieve data from the server every second 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 ); // Convert the start time to the timestamp $ end_famate_time = strtotime ($ end_time); // Convert the end time to the timestamp $ now_time = time (); if ($ end_famate_time <$ now_time | $ start_time> $ now_time) {echo 'is not within the test time range! '; Exit ;}$ remain_time = $ end_famate_time-$ now_time; // the remaining seconds $ remain_hour = floor ($ remain_time/(60*60 )); // remaining hour $ remain_minute = floor ($ remain_time-$ remain_hour * 60*60)/60 ); // remaining minutes $ remain_second = ($ remain_time-$ remain_hour * 60*60-$ remain_minute * 60 ); // the remaining seconds echo json_encode (array ('hour' => $ remain_hour, 'Minute '=> $ remain_minute, 'second' => $ remain_second ));

The above is the key code for implementing the countdown in php, and I hope it will be helpful for everyone's learning.

Articles you may be interested in:
  • Insert the current time in php MYSQL
  • PHP timestamp using instance code
  • Php calculates the time difference between two timestamps (hours)
  • Php set_time_limit (0) function for setting the program execution time
  • Php date to timestamp, specifying date to timestamp
  • Php + js countdown
  • Php to precisely set the session expiration time
  • PHP method for obtaining timestamp in milliseconds

Today, many group buying websites have time remaining. Javascript can be used to display the remaining time, but we will find that Javascript is used to implement it...

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.