Home  >  Article  >  Backend Development  >  php中计算时间差的几种方法_PHP

php中计算时间差的几种方法_PHP

WBOY
WBOYOriginal
2016-06-01 12:22:29748browse

一个简单的例子就是计算借书的天数,这需要php根据每天的日期进行计算,下面就来谈谈实现这种日期计算的几种方法:
(1) 如果有数据库就很容易了!若是MSSQL可以使用触发器!用专门计算日期差的函数datediff()便可!
若是MYSQL那就用两个日期字段的差值计算的计算结果保存在另一个数值型字段中!用时调用便可!
(2)如果没有数据库,那就得完全用php的时间日期函数!下面主要说明之:
例:计算1998年5月3日到1999-6-5的天数:
$startdate=mktime("0","0","0","5","3","1998");
$enddate=mktime("0","0","0","6","5","1999");
//所得到的值为从1970-1-1到参数时间的总秒数:是整数.那么

//下面的代码就好编多了:
$days=round(($enddate-$startdate)/3600/24) ;
echo $days;
//days为得到的天数;
?>
若mktime()中的参数缺省,那表示使用当前日期,这样便可计算从借书日期至今的天数.

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