-1

I'm having a (small) issue regarding a time calculator in PHP. The problem is the following:

I have four dates (all on the same day) but different hours. There is a group A and a group B which is like:

GROUP A:

from 9:00 till 12:00

GROUP B:

from 10:30 till 12:30

What I need is a way to check if group b falls in group a (which it does for 2 hours), and if it falls in group a then it has to calculate for how many hours it does. After that it has to add the remaining hours that were not overlapsing (which is 12:00 till 12:30 so 30 minutes).

So the answer of this calculation should be 2.5 hours, because group B was in group A for 2 hours and then it had a half hour (0.5) out of group A.

I hope that someone can help me with this because I have no idea how to calculate this in PHP.

Amit Rajput
  • 2,061
  • 1
  • 9
  • 27
Raflesia
  • 55
  • 1
  • 9
  • 1
    Please check this link http://stackoverflow.com/questions/32989461/overlapping-intervals-and-the-amount-of-overlaps – Tejas Patil Feb 01 '16 at 12:16

1 Answers1

0

it will be 2, because group B was in group A for 1.5 hours not 2 hours:

$time1_1 = '01-02-2016 9:00';
$time1_2 = '01-02-2016 12:00';
$time2_1 = '01-02-2016 10:30';
$time2_2 = '01-02-2016 12:30';

$hourdiff1 = round((strtotime($time1_2) - strtotime($time2_1))/3600, 1);
$hourdiff2 = round((strtotime($time2_2) - strtotime($time1_2))/3600, 1);

$hourdiff = $hourdiff1 + $hourdiff2;
echo $hourdiff;

and this is reference.

Community
  • 1
  • 1
Gouda Elalfy
  • 6,888
  • 1
  • 26
  • 38