PHP - 时差(HH:MM 格式) [英] PHP - hours difference (HH:MM format)

查看:24
本文介绍了PHP - 时差(HH:MM 格式)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试计算在这里工作的人员的轮班模式,从结束时间中减去开始时间在大多数情况下有效,但如果他们通宵工作则无效.例如,某人从 晚上 10 点到早上 6 点工作 将显示为:

I'm trying to calculate the shift patterns of people who work here, subtracting the start time from the end time works for the most part, but not if they're working overnight. For example someone working from 10pm to 6am would be displayed as:

22:00 - 06:00

我希望返回 8 小时,但我只是想不出最好的方法.

I'd like that to return 8 hours, but I just can't figure out the best way of doing it.

恼人的是,我将不得不在 Javascript 中做完全相同的事情,所以我希望我能理解这个逻辑.在这种情况下,从另一个约会中删除一个约会是行不通的...

Annoyingly, I'm then going to have to do the exact same thing in Javascript, so I hope I can get my head around the logic for this. Taking away one date from the other just doesn't work in this case...

任何帮助将不胜感激,谢谢!

Any help would be much appreciated, thank you!

推荐答案

function timeDiff($firstTime,$lastTime) {
    $firstTime=strtotime($firstTime);
    $lastTime=strtotime($lastTime);
    $timeDiff=$lastTime-$firstTime;
    return $timeDiff;
}

echo (timeDiff("10:00","12:00")/60)/60;

最初在这里写的:https://ao.ms/how-to-get-the-hours-difference-in-hhmm-format-in-php/

这篇关于PHP - 时差(HH:MM 格式)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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