计算两个时间戳之间的总小时数 [英] Calculate total hours between 2 time stamps

查看:661
本文介绍了计算两个时间戳之间的总小时数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个变量:

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.

这篇关于计算两个时间戳之间的总小时数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆