从重叠时间php计算小时的最短逻辑

时间:2014-09-15 11:07:26

标签: php

我正在尝试计算用户的小时数。如果有很多时间表分配给他。

时间表是 -

10:00 AM - 12 pm
11 am - 12 pm
9 am - 12 pm
8 am - 5 pm

将这些计划分配给用户。 我不得不计算他花在这些时间表上的时间。

有没有最短的计算方法?

我想回答= 8小时。我不想计算我想要的小时数。

不,我想要结果8 ..我解释为什么
上午10:00 - 下午12点--- 2小时 上午11点至下午12点----已经加入1小时, 上午9点 - 下午12点----- 3小时但10 - 12已经添加了1小时, 上午8点 - 下午5点---- 8小时

2 个答案:

答案 0 :(得分:1)

使用此:

$minutes_diff = round(abs(strtotime($time1) - strtotime($time2)) / 60);

或者这个:

$time1="7.30 AM";
$time2="8.30 PM";

$d1=  strtotime($time1);
$d2=  strtotime($time2);

$diff=$d2-$d1;

//Print the difference in hours : minutes
echo date("H:i",$diff);

答案 1 :(得分:0)

我写了这个脚本来计算从工作日开始到结束的工作时间表中的总小时数:

// $key['date'] contains a date format of 'yyyy-mm-dd'
// $key['start'] and $key['end'] contains a time of format hh:mm:ss             
$start = new DateTime( $key['date'] .'T' . $key['start'] );
$end = new DateTime( $key['date'] .'T' . $key['end'] );

// difference returns the differential between start and end resulting in number of hours worked
$difference = $start->diff( $end );

// Additionally, I would like 1 hour and 15 minutes to result in 1.25 instead of 1.15 and so on. For this I used this:
$minute = $difference->format( '%i' )*1.666666666667;
$total = $difference->format( '%h' ) . '.' . $minute;