Solution in PHP
In my view, since we're working with schedules, the easiest is to use the DateTime class. of PHP. First, let's define the tests shown in the statement:
$tests = [
[
"inicio" => "07:00:00",
"final" => "09:00:00",
"busca" => "08:00:00",
"saida" => true
],[
"inicio" => "19:00:00",
"final" => "22:00:00",
"busca" => "23:00:00",
"saida" => false
],[
"inicio" => "18:00:00",
"final" => "03:00:00",
"busca" => "01:00:00",
"saida" => true
],
];
The three tests proposed, consisting of the initial, final, search and expected time. To run the tests, we use a basic loop :
foreach($tests as $test)
{
// ...
}
First step is to define the time-related objects:
foreach($tests as $test)
{
$ininio = new DateTime($test["inicio"]);
$final = new DateTime($test["final"]);
$busca = new DateTime($test["busca"]);
}
As stated in the statement it is clear that it should be independent of the day and that even the interval can start in one day and end in another, as in the case of the third test, we need to do a simple check: final is less than the initial, add at the end an interval of one day.
foreach($tests as $test)
{
$ininio = new DateTime($test["inicio"]);
$final = new DateTime($test["final"]);
$busca = new DateTime($test["busca"]);
if ($final <= $inicio) {
$final->add(new DateInterval("P1D"));
}
}
Read more about the class DateInterval in the documentation. This way, if the final time is less than the initial time, it is added 24h in it, and it will be the same time the next day.
The same logic applies to the search time: if it is less than the start time, it should be considered as the next day and therefore added 24 hours as well.
foreach($tests as $test)
{
$ininio = new DateTime($test["inicio"]);
$final = new DateTime($test["final"]);
$busca = new DateTime($test["busca"]);
if ($final <= $inicio) {
$final->add(new DateInterval("P1D"));
}
if ($busca <= $inicio) {
$busca->add(new DateInterval("P1D"));
}
}
With this, just check the range:
foreach($tests as $test)
{
$ininio = new DateTime($test["inicio"]);
$final = new DateTime($test["final"]);
$busca = new DateTime($test["busca"]);
if ($final <= $ininio) {
$final->add(new DateInterval("P1D"));
}
if ($busca <= $ininio) {
$busca->add(new DateInterval("P1D"));
}
if ($busca >= $ininio && $busca <= $final) {
echo "Sim";
} else {
echo "Não";
}
echo ", esperado " . ($test["saida"] ? "sim" : "não") . PHP_EOL;
}
I added the expected value for each test to the output message for comparison. When executing the code, we will have the output:
Sim, esperado sim
Não, esperado não
Sim, esperado sim
See the code working at Repl.it or Ideone .