php实现倒计时效果,
php实现倒计时效果,
现在很多的团购网站上都有剩余时间的显示。显示剩余时间可以使用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布局
考试剩余时间:复制代码 代码如下:00小时00分00秒
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 MYSQL中插入当前时间
- PHP时间戳使用实例代码
- php 计算两个时间戳相隔的时间的函数(小时)
- php set_time_limit(0) 设置程序执行时间的函数
- php日期转时间戳,指定日期转换成时间戳
- php+js实现倒计时功能
- php中实现精确设置session过期时间的方法
- PHP获取毫秒级时间戳的方法

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7
