现在很多的团购网站上都有剩余时间的显示。显示剩余时间可以使用Javascript来实现,但是我们会发现使用Javascript来实现的话不安全,因为Javascript获取的是客户端的时间。比方说,这次团购已经结束了,但是懂技术的访客只要修改他客户端电脑的时间就又显示该商品还可以购买。很明显,这不是我们网站设计的初衷。团购结束了就不可以再购买啦。这里写了一个考试系统剩余时间倒计时的显示代码和大家一起探讨。
实现原理:
PHP获取的是服务器端的时间,我们只要设置考试的开始时间和结束时间及当前时间。如果当前的时间不在我们的考试时间范围内,则显示给考生“当前不在考试时间段!”。如果在考试时间范围内,则获取当前时间,结束时间减掉当前时间就是剩余的时间,将剩余时间格式化输出就是“剩余考试时间:2小时56分32秒”这样的形式。服务器端获得了剩余时间后,我们还要在客户端动态的显示剩余时间的倒计时。这就需要用AJAX来实现了。在开始前大家先熟悉几个函数吧!
PHP函数:
strtotime(); //将任何英文日期转换为时间戳
floor(); //舍去法取整,和int()强制转换相识
json_encode() //对变量进行JSON编码,返回字符串
简单的剩余天数计算:
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).'天';
效果:
简单的剩余时间计算:
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).'分钟下班';
效果:
前后端配合实现考试剩余时间:
HTML布局
考试剩余时间:
JS脚本
function dealData(id,value){ var place = document.getElementById(id); place.innerHTML = value; } window.setInterval(function(){ // 每秒从服务器取一次数据 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代码:
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));
以上就是php实现倒计时的关键代码,希望对大家的学习有所帮助。

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download
The most popular open source editor

Dreamweaver Mac version
Visual web development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.
