计算php中2个时间戳之间的差异

新手上路,请多包涵

我在表中使用了 2 个时间戳,即 starttime 数据类型 - 时间戳和当前时间戳。 endtime 数据类型-时间戳,默认为 0000-00-00 00:00:00

如何计算 php starttime:2016-11-30 03:55:06 endtimetime: 2016-11-30 11:55:06 中 2 个时间戳之间的差异

原文由 user6582683 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 524
1 个回答

如果您需要 00:00:00(时、分、秒) 格式的差异,可以使用此功能。

我使用 sprintf() 因为在一位数结果之前添加零。

 function timeDifference($start, $stop){

$diff = strtotime($stop) - strtotime($start);
$fullHours   = floor($diff/(60*60));
$fullMinutes = floor(($diff-($fullHours*60*60))/60);
$fullSeconds = floor($diff-($fullHours*60*60)-($fullMinutes*60));

return sprintf("%02d",$fullHours) . ":" . sprintf("%02d",$fullMinutes) . ":" . sprintf("%02d",$fullSeconds);
}

原文由 sezer iltekin 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题