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( "2011-10-28" ), strtotime( "2011-10-29" ) );
print_r( $timediff );

还有

<?php
$one = strtotime('2011-12-08 07:02:40');//开始时间 时间戳
$tow = strtotime('2011-12-25 00:00:00');//结束时间 时间戳
$cle = $tow - $one; //得出时间戳差值

/* 这个只是提示
echo floor($cle/60); //得出一共多少分钟
echo floor($cle/3600); //得出一共多少小时
echo floor($cle/3600/24); //得出一共多少天
*/
/*Rming()函数,即舍去法取整*/
$d = floor($cle/3600/24);
$h = floor(($cle%(3600*24))/3600);  //%取余
$m = floor(($cle%(3600*24))%3600/60);
$s = floor(($cle%(3600*24))%60);

echo "两个时间相差 $d$h 小时 $m$s 秒"

 

posted @ 2016-09-13 09:16  壁虎漫步.  阅读(1589)  评论(0编辑  收藏  举报