Home > php教程 > php手册 > 关于时间计算的结总

关于时间计算的结总

WBOY
Release: 2016-06-13 12:35:30
Original
843 people have browsed it

php中计算时间差有时候是件很麻烦的事! 

不过我相信任何语言,只要撑握了其中规率就能找到办法,绝对是化腐朽为神奇的。 

工作中经常要进行日期计算,下面总结几个工作中总结出的经验。先记下免得日后会忘! 

1。第一种 计算时间段差(可以是分,秒,天) 

$endtime="2004-09-09 18:10:00"; 
$d1=substr($endtime,17,2); //秒 
$d2=substr($endtime,14,2); //分 
$d3=substr($endtime,11,2); // 时 
$d4=substr($endtime,8,2);  //日 
$d5=substr($endtime,5,2); //月 
$d6=substr($endtime,0,4); //年 

echo $d1.'-'.$d2.'-'.$d3.'-'.$d5.'-'.$d4.'-'.$d6."n"; 

echo date("Y-m-d H:i:s")."n"; 
$now_T=mktime(date("H"),date("i"),date("s"),date("m"),date("d"),date("Y"));  
echo $now_T."n"; 
$now_S=mktime("$d3","$d2","$d1","$d5","$d4","$d6"); 
echo $now_S."n"; 
 $end_TS=($now_S-$now_T)/60;   //计算  剩余分钟 
echo $end_TS; 
?>  

注 $startdate=mktime("0","0","0","1","1","2000");  
   //所得到的值为从1970-1-1到参数时间的总秒数 那么 化为分 /60   时 /3600   天/3600/24  即可!     

若mktime()中的参数缺省,那表示使用当前日期。 


2.  如果有数据库就很容易了!若是MSSQL可以使用触发器!用专门计算日期差的函数datediff()便可!  
若是MYSQL那就用两个日期字段的差值计算的计算结果保存在另一个数值型字段中!用时调用便可!

source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template