计算两个时间戳之间的总小时数

计算两个时间戳之间的总小时数

问题描述:

我有两个变量:

I have two variables:

$start_time = '9:36';
$end_time = '12:47';

我如何计算两次之间的小时数并将它们存储在如下变量中:

How would i calculate the amount of hours between the two times and store them in a variable like this:

$total_hours = 3;

我读过,您需要先转换时间。

I have read that you need to convert the times first. Also there will be no need for date as this will all be on the same day.

任何帮助将不胜感激

这解决了您的问题:

This solves your problem:

$start = explode(':', $start_time);
$end = explode(':', $end_time);
$total_hours = $end[0] - $start[0] - ($end[1] < $start[1]);

请参阅此键盘作为证明。

此解决方案假定两个小时都是同一天。

This solution assumes both hours are from the same day.