0

I allow myself to ask my question here, because after several hours of trying to make my code work, it still does not work! So I would simply like to check if two times (For example 8:00 and 10:00) are included between a time range (For example between 8:00 and 9:00). I have already tried several combinations, it seems to work from time to time, but when I check if 9:00 and 10:00 is between 8:00 and 9:00, it returns that the time is not included in the time slot when it is supposed to be! Here is my code for the moment: (all data has been transformed into strtotime())

if ((($hdebut >= $ihdebutcours) && ($hdebut <= $ihfincours)) && (($hfin >= $ihdebutcours) && ($hfin >= $ihfincours))) {
    $f++;
}

For example :

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

Thank you very much to the people who will help me, it will help me enormously to continue my project, and sorry for my bad English! Good day to you !

Syscall
  • 19,327
  • 10
  • 37
  • 52
Jojo_14
  • 17
  • 1
  • 7
  • `($hfin >= $ihfincours)` condition is not satisfied that's why it returns false – Ankit Jindal Feb 06 '22 at 14:02
  • No, unfortunately this still does not work! – Jojo_14 Feb 06 '22 at 14:17
  • Do you know Carbon? This answer might help you https://stackoverflow.com/questions/39508963/calculate-difference-between-two-dates-using-carbon-and-blade – Anthony Meinder Feb 06 '22 at 14:19
  • No, not at all, and do you think it can help me to check if two hours are between two other hours? – Jojo_14 Feb 06 '22 at 14:28
  • You should probably consider converting to `DateTime` objects, or normalize to minutes, or something. Please see https://3v4l.org/COqlK for how strings are compared. ```var_dump( '9:00' > '10:00', // true '10:00' > '9:00', // false '10:00' > '11:00', // false '11:00' > '10:00', // true );``` etc. so that doesn't really add up does it. – Markus AO Feb 06 '22 at 14:31
  • If you're wondering why that is, `>` and `<` with strings, unless they can be gracefully coerced to numbers (which isn't the case here), is comparison of alphabetical order. ```9:00 > 10:00``` because 9 comes after 1 in the scheme of 0123456789 things. https://stackoverflow.com/questions/12888674/how-does-php-compare-strings-with-comparison-operators – Markus AO Feb 06 '22 at 14:34
  • Yes, but all data is converted to strtotime(), I put '9:00' or '10:00' to give you an example! – Jojo_14 Feb 06 '22 at 14:37

3 Answers3

1

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.

Simon K
  • 1,503
  • 1
  • 8
  • 10
  • Does this have any real difference with strtotime which I already use? Because by doing this, there is no difference unfortunately! – Jojo_14 Feb 06 '22 at 14:43
  • Thats odd. When I tested my solution before posting, I initially set `$f` to `0` and ran the code. The result was`$f` was `1`. - i.e. - it worked – Simon K Feb 06 '22 at 14:50
0

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)) {
      //...
  }
Anthony Meinder
  • 191
  • 4
  • 11
0

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)
Jojo_14
  • 17
  • 1
  • 7