在放代码之前,我得先说一下,无论是计算2016-08-01到2017-08-01的时间差,还是2017-08-01到2016-08-01的时间差,返回的都将是365天,不会出现正负数的情况,请知悉!下面上代码:
//PHP计算两个时间差的方法 function timeDiff( $begin_time, $end_time ) { if ( $begin_time < $end_time ) { $starttime = $begin_time; $endtime = $end_time; } else { $starttime = $end_time; $endtime = $begin_time; } $timediff = $endtime - $starttime; $days = intval( $timediff / 86400 ); $remain = $timediff % 86400; $hours = intval( $remain / 3600 ); $remain = $remain % 3600; $mins = intval( $remain / 60 ); $secs = $remain % 60; $res = array( "day" => $days, "hour" => $hours, "min" => $mins, "sec" => $secs ); return $res; } //======== 实例使用 ======== $timediff = timeDiff( strtotime( "2016-08-01" ), strtotime( "2017-08-01" ) ); print_r( $timediff );
有了这个,你就可以随意计算时间差咯~
没有难的技术,当你弄清它的原理时,你会发现原来如此简单~ 欢迎加群【536501561】讨论