One - One Code All

Blog Content

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

PHP   2012-05-11 19:26:40

做减法得到值是相差的秒数,这个秒数对86400(一天的秒数)取余,则得到相差数。如果对86400取模,还对3600秒、60秒取余,则得到相关的小时和分钟数。如果对86400取模,再对60取模,则得到相差的秒数。

$startdate="2011-3-15 11:50:00";
 
$enddate="2012-5-11 12:12:12";
 
$date=floor((strtotime($enddate)-strtotime($startdate))/86400);
echo "相差天数:".$date."天

";   $hour=floor((strtotime($enddate)-strtotime($startdate))%86400/3600); echo "相差小时数:".$hour."小时

";   $minute=floor((strtotime($enddate)-strtotime($startdate))%86400/60); echo "相差分钟数:".$minute."分钟

";   $second=floor((strtotime($enddate)-strtotime($startdate))%86400%60); echo "相差秒数:".$second."秒";



上一篇:php判断key是否存在的两种方法
下一篇:php获取数组所有的key,数组的key组成数组

The minute you think of giving up, think of the reason why you held on so long.