• 技术文章 >php教程 >php手册

    简单的PHP实现倒计时方法

    2016-06-06 19:46:14原创1442

    /** * @todo Count Down (倒 计时 ) * @param String $endTime * @return String time * @example * $endTime = 2014-07-13 8:15:00;echo countDown($endTime); */function countDown($endTime){$endTime = strtotime($endTime);$beiginTime = strtotime(dat

    	/**
    	 * @todo Count Down (倒计时)
    	 * @param String $endTime
    	 * @return String time
    	 * @example 
    	 * 	$endTime = '2014-07-13 8:15:00';
    		echo countDown($endTime);
    	 */
    	function countDown($endTime){
    		$endTime = strtotime($endTime);
    		$beiginTime = strtotime(date('Y-m-d H:i:s'));
    		$timeDifference = $endTime - $beiginTime;
    		switch ($timeDifference){
    			case $timeDifference < 0 :
    				 $timeDifference = '已经结束!';
    				 break;
    			case $timeDifference < 60 :
    				 $timeDifference = $timeDifference.'秒';
    				 break;
    			case $timeDifference < 3600 :
    				 $minutes = floor($timeDifference/60);
    				 $seconds = floor($timeDifference - ($minutes*60));
    				 $timeDifference = $minutes.'分'.$seconds.'秒';
    				 break;
    			case $timeDifference < 86400 :
    				 $hours = floor($timeDifference/3600);
    				 $minutes = floor(($timeDifference - ($hours*3600))/60);
    				 $seconds = floor($timeDifference - ($hours*3600) - ($minutes*60));
    				 $timeDifference = $hours.'小时'.$minutes.'分'.$seconds.'秒';
    				 break;
    			default:
    				 $days = floor(($timeDifference/86400));
    				 $hours = floor(($timeDifference - ($days*86400))/3600);
    				 $minutes = floor(($timeDifference - ($days*86400) - ($hours*3600))/60);
    				 $seconds = floor($timeDifference - ($days*86400) - ($hours*3600) - ($minutes*60));
    				 $timeDifference = $days.'天'.$hours.'小时'.$minutes.'分'.$seconds.'秒';
    				 break;
    		}
    		return $timeDifference;
    	}

    方法可以让整个计算在PHP后端计算并且输出一定的格式后直接在页面展示倒计时详细信息。

    页面想要实现1秒自动刷新请配合ajax和定时器去进行局部刷新即可。

    此处主要的是描述一个思路,至于怎么做好还是要看项目和具体的需求。

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    上一篇:PHP文件上传涉及到的一些参数:post 下一篇:php split() 函数的用法
    20期PHP线上班

    相关文章推荐

    • 【活动】充值PHP中文网VIP即送云服务器• PHP+TEXT留言本(三)• Smarty在模板文件中的一些应用• 完美解决令人抓狂的zend studio 7代码提示(content Assist)速度慢的问题• Linux下CoreSeek及PHP扩展模块的安装• php 中文处理函数集合
    1/1

    PHP中文网