php 计算两个时间相差的天数、小时数、分钟数、秒数详解及实例代码

点击:- 发布时间:2020-12-29 15:15:45

php计算两个时间相差的天数、小时数、分钟数、秒数


PHP中计算两个时间相差的天数、小时数、分钟数、秒数不如其它语言方便,但搞清了PHP中时间的表示方法后也很简单。本文章向大家讲解php如何计算时间相差。需要的码农可以参考一下。


先看下面这个网上给的例子:

//$startdate是开始时间,$enddate是结束时间
<?php
$startdate="2011-3-15 11:50:00";
 
$enddate="2012-12-12 12:12:12";
 
$date=floor((strtotime($enddate)-strtotime($startdate))/86400);
echo "相差天数:".$date."天<br/><br/>";
 
$hour=floor((strtotime($enddate)-strtotime($startdate))%86400/3600);
echo "相差小时数:".$hour."小时<br/><br/>";
 
$minute=floor((strtotime($enddate)-strtotime($startdate))%86400/60);
echo "相差分钟数:".$minute."分钟<br/><br/>";
 
$second=floor((strtotime($enddate)-strtotime($startdate))%86400%60);
echo "相差秒数:".$second."秒";
?>


相关内容
精彩推荐