Home >Backend Development >PHP Tutorial >Implement countdown effect in php, _PHP tutorial

Implement countdown effect in php, _PHP tutorial

WBOY
WBOYOriginal
2016-07-12 09:02:291001browse

php implements the countdown effect,

Now many group buying websites have the remaining time displayed. Displaying the remaining time can be implemented using Javascript, but we will find that it is unsafe to use Javascript because Javascript obtains the client's time. For example, this group purchase has ended, but as long as a technically savvy visitor changes the time on his client computer, it will show that the product is still available for purchase. Obviously, this is not the original intention of our website design. Once the group purchase is over, you can no longer purchase it. Here I wrote a display code for the remaining time countdown of the exam system and discuss it with everyone.

Implementation principle:
PHP obtains the time on the server side. We only need to set the start time, end time and current time of the exam. If the current time is not within our examination time range, it will display to the candidate "We are not currently in the examination time period!". If it is within the exam time range, the current time is obtained. The end time minus the current time is the remaining time. The remaining time is formatted and output in the form of "remaining exam time: 2 hours, 56 minutes and 32 seconds". After the server side obtains the remaining time, we also need to dynamically display the countdown of the remaining time on the client side. This needs to be achieved using AJAX. Before we start, let’s get familiar with a few functions!

PHP function:
strtotime(); //Convert any English date to a timestamp
floor(); //The rounding method is similar to int() forced conversion
json_encode() //JSON-encode the variable and return the string

Simple days remaining calculation:

date_default_timezone_set('Asia/Hong_Kong');
$startDate = '2015-8-11';
$endDate = '2015-8-31';

// 将日期转换为Unix时间戳
$startDateStr = strtotime($startDate);
$endtDateStr = strtotime($endDate);
$total = $endtDateStr-$startDateStr;

$now = strtotime(date('Y-m-d'));
$remain = $endtDateStr-$now;

echo '为期:'.$total/(3600*24).'天<br>';
echo '剩余:'.$remain/(3600*24).'天';

Effect:

Simple remaining time calculation:

date_default_timezone_set('Asia/Hong_Kong');
$startTime = '09:00:00';
$endTime = '18:00:00';

// 将时间转化为unix时间戳
$startTimeStr = strtotime($startTime);
$endTimeStr = strtotime($endTime);
$total = $endTimeStr - $startTimeStr;

$restHours = 1; // 休息1小时

$now = strtotime(date('H:i:s'));
$remain = $endTimeStr - $now;

echo '上班时间:'.($total/3600-$restHours).'小时<br>';
echo '还有:'.floor(($remain/3600)).'小时'.floor($remain/60).'分钟下班';

Effect:

The front-end and back-end cooperate to realize the remaining time of the exam:

HTML Layout

Remaining time for exam: Copy code The code is as follows:a03c7d6d3ca32a3a76fed68a309ebd1a0054bdf357c58b8a65c66d7c19c8e4d114hour18d92ef0689969af4692cbeab2bef7e5 0054bdf357c58b8a65c66d7c19c8e4d114minutesc7e9691220380280e80f2ec34b521e1e0054bdf357c58b8a65c66d7c19c8e4d114seconds

JS script

function dealData(id,value){
  var place = document.getElementById(id);
  place.innerHTML = value;
}
window.setInterval(function(){ // 每秒从服务器取一次数据
  var ajax = new Ajax();
  ajax.get("remain_time.php&#63;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);//开始时间转化为时间戳
$end_famate_time = strtotime($end_time); //结束时间转化为时间戳
$now_time = time();
if($end_famate_time < $now_time || $start_time > $now_time){
 echo '当前不在考试的时间范围内!';
 exit;
}
$remain_time = $end_famate_time-$now_time; //剩余的秒数
$remain_hour = floor($remain_time/(60*60)); //剩余的小时
$remain_minute = floor(($remain_time - $remain_hour*60*60)/60); //剩余的分钟数
$remain_second = ($remain_time - $remain_hour*60*60 - $remain_minute*60); //剩余的秒数
echo json_encode(array('hour'=>$remain_hour,'minute'=>$remain_minute,'second'=>$remain_second));

The above is the key code for PHP to implement countdown. I hope it will be helpful to everyone's learning.

Articles you may be interested in:

  • Insert current time in php MYSQL
  • PHP timestamp using example code
  • php calculates two timestamps Function of interval (hours)
  • php set_time_limit(0) Function to set program execution time
  • php date to timestamp, specified date to timestamp
  • php JS implements countdown function
  • Method to accurately set session expiration time in PHP
  • PHP method to obtain millisecond timestamp

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1084600.htmlTechArticlephp realizes the countdown effect. Nowadays, many group buying websites have the remaining time displayed. Displaying the remaining time can be implemented using Javascript, but we will find that using Javascript...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn