How to count how many hours inside an hour range?

半腔热情 提交于 2020-01-06 04:12:06

问题


Can you help me with this?

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?

I have a code like this:

<?php

$start = "13:15:00";
$end = "18:30:00";

$time = strtotime($start);
$timeStop = strtotime($end);

//how can I count the hours inside?

?>

回答1:


You could use gmdate() in this case:

$time = gmdate('H:i:s', $timeStop - $time); // feed seconds
echo $time;

Or with DateTime class also:

$time = new DateTime($start);
$timeStop = new DateTime($end);
$diff = $timeStop->diff($time);
echo $diff->format('%h:%i'); // hours minutes



回答2:


<?php

$start = "13:15:00";
$end = "18:30:00";

$time = strtotime($start);
$timeStop = strtotime($end);

//To count hours in the range
$diff = intval(($timeStop - $time)/3600);
//3600 refers to 1 hour

?>

So you take the difference between two timestamps and divide by 3600 seconds to get the number of hours. intval is to get an integer value. You are free to use round, ceil or floor.



来源:https://stackoverflow.com/questions/26092297/how-to-count-how-many-hours-inside-an-hour-range

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!