如何计算一个小时范围内的小时数? [英] How to count how many hours inside an hour range?
本文介绍了如何计算一个小时范围内的小时数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你能帮我解决这个问题吗?
Can you help me with this?
我正在创建一个活动策划者,我有一个由小时范围组成的函数.例如,我有一个事件 BIRTHDAY,这个事件从 13:15:00 到 18:30:00 开始.现在我如何计算这个范围内的小时数?
I am creating an event planner and I have a function that consist of hour range. For example I have an event BIRTHDAY and this event is started from 13:15:00 hours to 18:30:00 hours. Now how can I count the hours inside this range?
我有这样的代码:
<?php
$start = "13:15:00";
$end = "18:30:00";
$time = strtotime($start);
$timeStop = strtotime($end);
//how can I count the hours inside?
?>
推荐答案
你可以在这种情况下使用 gmdate()
:
You could use gmdate()
in this case:
$time = gmdate('H:i:s', $timeStop - $time); // feed seconds
echo $time;
或者也使用 DateTime
类:
$time = new DateTime($start);
$timeStop = new DateTime($end);
$diff = $timeStop->diff($time);
echo $diff->format('%h:%i'); // hours minutes
这篇关于如何计算一个小时范围内的小时数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文