关于PHP如何计算两个时间之前的差值,见代码:
1 /** 2 * 计算时间差 3 * @param int $timestamp1 时间戳开始 4 * @param int $timestamp2 时间戳结束 5 * @return array 6 */ 7 function time_diff($timestamp1, $timestamp2) 8 { 9 if ($timestamp2 <= $timestamp1) 10 { 11 return ['hours'=>0, 'minutes'=>0, 'seconds'=>0]; 12 } 13 $timediff = $timestamp2 - $timestamp1; 14 // 时 15 $remain = $timediff%86400; 16 $hours = intval($remain/3600); 17 18 // 分 19 $remain = $timediff%3600; 20 $mins = intval($remain/60); 21 // 秒 22 $secs = $remain%60; 23 24 $time = ['hours'=>$hours, 'minutes'=>$mins, 'seconds'=>$secs]; 25 26 return $time; 27 }
测试用例:
$time1 = 1478483626;
$time2 = 1478485126;
var_dump(time_diff($time1, $time2));
输出结果:
array(3) { ["hours"]=> int(0) ["minutes"]=> int(25) ["seconds"]=> int(0) }
什么天之类的类似方法,注意时分秒之间的倍数是60就好计算了。