Detailed code explanation of several methods for calculating time difference in php
怪我咯
Release: 2023-03-13 10:00:02
Original
1518 people have browsed it

在php中计算时间差有时候是件麻烦的事!不过只要你掌握了日期时间函数的用法那这些也就变的简单了

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

<?php
$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为得到的天数; 
?>
Copy after login

若mktime()中的参数缺省,那表示使用当前日期,这样便可计算从借书日期至今的天数.

The above is the detailed content of Detailed code explanation of several methods for calculating time difference in php

Related labels:
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template