php - Check if two hours are between a time range

Solution:

Try PHP's DateTime...

<?php

$hdebut = '9:00';
$hfin = '11:15';
$ihdebutcours = '9:00';
$ihfincours = '10:00';

$hdebutDT = new DateTime($hdebut);
$hfinDT = new DateTime($hfin);
$ihdebutcoursDT = new DateTime($ihdebutcours );
$ihfincoursDT = new DateTime($ihfincours);

if ((($hdebutDT >= $ihdebutcoursDT) && ($hdebutDT <= $ihfincoursDT)) && (($hfinDT >= $ihdebutcoursDT) && ($hfinDT >= $ihfincoursDT))) {
    $f++;
}

You could even shorten all this by using DateTime::diff also perhaps.

Answer

Solution:

It sure is overkilled but when dealing with dates, I often go with Carbon.

With Carbon, you could easily check if a date is in the timerange of two dates

  $lessonStart = \Carbon\Carbon::parse('today at 9:00');
  $lessonEnd = \Carbon\Carbon::parse('today at 11:15');

  $arrivedAt = \Carbon\Carbon::parse('today at 9:00');
  $endedAt = \Carbon\Carbon::parse('today at 10:00');

  if ($arrivedAt->between($lessonStart, $lessonEnd) && $endedAt->between($lessonStart, $lessonEnd)) {
      //...
  }

Answer

Solution:

I just found the problem! It was actually the operator! I changed it to "<" instead of "<="! Thank you all for your messages, and again sorry to have disturbed you! Good day to you !

($hdebut <= $ihdebutcours AND $ihdebutcours < $hfin) OR ($hdebut < $ihfincours AND $ihfincours <= $hfin)

Source