问题
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