gpt4 book ai didi

php - 计算夜类时间记录的时间差

转载 作者:可可西里 更新时间:2023-10-31 23:48:37 25 4
gpt4 key购买 nike

我正在计算仅使用时间的夜类时间表的时差

假设我有这些数据:

 $actual_in_time      = 6:45 PM        //date July 30, 2013
$actual_out_timeout = 7:00 AM //date July 31, 2013

我必须计算时间差,其中的时间应该转换为一个完整的时间,因此

 $actual_in_time     = //(some code to convert 6:45 PM to 7:00 PM)
$converted_in_time = $actual_in_time;

现在这是我的代码:

 $actual_out_time   += 86400;
$getInterval = $actual_out_time - $converted_in_time;
$hours = round($getInterval/60/60, 2, PHP_ROUND_HALF_DOWN);
$hours = floor($hours);

我没有得到想要的结果。您如何计算以时间为基础的时差?

最佳答案

使用 DateTime 对象

$start =  new DateTime('2000-01-01 6:45 PM');
$end = new DateTime('2000-01-01 7:00 AM');
if ($end<$start)$end->add(new DateInterval('P1D'));
$diff = date_diff($start,$end);
echo $diff->format('%h hours %i minutes');

如果您的结束时间小于开始时间,则增加 1 天。

关于php - 计算夜类时间记录的时间差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17965013/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com