1

I am having difficulties seeing "how much" of 2 DateTime's are between 2 other date times.

For instance, i have a "start" and "end" datetime (i omit the day, seeing as only the time is relevant here):

Start: 01:00
End: 09:30

And i have 2 other times:

Night_start: 00:00
Night_end: 04:00

So, i need to figure out "how much" of the "start" and "end" are between the "night_start" and "night_end" part.

The correct answer is obviously 3 hours, but how would i go about calculating this?

Hope my question makes sense. thanks.

J.B.J.
  • 440
  • 1
  • 8
  • 15
  • Does this answer your question? [How do I find the hour difference between two dates in PHP?](https://stackoverflow.com/questions/3763476/how-do-i-find-the-hour-difference-between-two-dates-in-php) – Bhaumik Pandhi Aug 22 '22 at 11:04
  • No, because it compares the hour difference between 2 dates - i kinda need to know how much a given date-period is "within" another given date period - so i am comparing 4 dates or 2 date-periods, and need to know how much "overlap" there is – J.B.J. Aug 22 '22 at 11:08
  • 3
    Maybe I'm being blind, but why is the answer 3.5 hours? It looks like there's only three hours overlapping between the two periods (01:00 -> 04:00). The answer should be something like `min(end, night-end) - max(start, night-start)`, unless I'm missing something. – iainn Aug 22 '22 at 11:09
  • you are totally right, the answer is 3 hours xD – J.B.J. Aug 22 '22 at 11:27

2 Answers2

1

So you're given 4 values and want to find the overlap.

sort them into order.

If the second value of the four is the last value of either of the input pairs the ranges do not overlap, else the overlap is the middle two values,

subtract to find the length of the overlap.

anoither way

given (as,ae),(bs,be) the overlap is

min(ae,be) - max(as,bs) when that result is positive, or no overlap for zero or negative.

you will need comparison and difference algorithms to work with your data types.

Jasen
  • 11,837
  • 2
  • 30
  • 48
0

With min(ae,be) - max(as,bs), and a time to decimal function. Something like that, then?

<?php
$start_date = '01:00:00';
$end_date = ' 09:30:00';
$start_night = '00:00:00';
$end_night = '04:00:00';

function timeToDecimal($time)
{
    $hms = explode(":", $time);
    return ($hms[0] + ($hms[1]/60) + ($hms[2]/3600));
}

$min = min(timeToDecimal($end_date), timeToDecimal($end_night));
$max = max(timeToDecimal($start_date), timeToDecimal($start_night));
$result = ($min-$max>0) ? $min-$max : timeToDecimal($end_night)-timeToDecimal($start_night);

echo  $result.' hour(s) of night';

Results:

3 hour(s) of night
Monnomcjo
  • 715
  • 1
  • 4
  • 14
  • 1
    I went with his "first" suggestion - i first checked to see if it was a night-shift, and if it was, i sorted the 4 values in order. Then, i took the middle 2 numbers and the difference between them was the overlap. This implementation was easier for me to do, since i was working with DateTime objects – J.B.J. Aug 23 '22 at 14:29