Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

156
Visualizações
Test if a range intersects another range of numbers

I have 2 range of numbers:

  • $startTime to $endTime
  • $offerStartTime to $offerEndTime

Each of the above variables are integers.

I want to see if the range offerStartTime to offerEndTime falls in the range startTime and endTime.

For example, if the startTime and endTime range was: 10 to 20, then the following example ranges would return true:

  • offerStartTime: 5, offerEndTime: 11
  • offerStartTime: 5, offerEndTime: 100
  • offerStartTime: 10, offerEndTime: 15
  • offerStartTime: 10, offerEndTime: 100
  • offerStartTime: 12, offerEndTime: 15
  • offerStartTime: 19, offerEndTime: 100

The following would return false:

  • offerStartTime: 1, offerEndTime: 3
  • offerStartTime: 90, offerEndTime: 100
  • offerStartTime: 1, offerEndTime: 10
  • offerStartTime: 20, offerEndTime: 100

How can I do this? Would ideally like answers in PHP, but pseudo code would be fine.

about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

If you are just checking whether any part of the offer overlaps any part of the range, it's simple.

if ($offerStartTime < $endTime && $offerEndTime > $startTime)  {
    echo 'The ranges overlap';
}

Here's a picture representing all the possibilities of overlap and non-overlap to visualize why this works.

enter image description here

Based on your inputs and expected false outputs, I used < and >. If you wanted to also include ranges that intersected at a point, you would need to use <= and >= instead.

about 4 years ago · Santiago Trujillo Relatório

0

//$startTime to $endTime
//$offerStartTime to $offerEndTime
//you can compare php times by using normal comparators so this is just a logic problem. here's the solution.


//ok let's start by making sure that neither offered time is within the range because if it is we KNOW it's already good so

if(($offerStartTime < $endTime && offerStartTime > $startTime) || ($offerEndTime < $endTime && offerEndTime > $startTime)){
      return true;
 }
 //so it's not easily already within the range so we have to test if the lower one is under the starting one but the other is above. ie.
elseif(($offerStartTime < $startTime) && ($offerEndTime > $startTime)){
     return true; 
}
//so the only other acceptable possibility is that the offered start time is lower than the endtime and the offered end time is higher ie
elseif(($offerStartTime < $endTime) && ($offerEndTime > $endTime)){
      return true;
}
//so we've exhausted all other valid possibilities it must be false
else{
      return false;
}
about 4 years ago · Santiago Trujillo Relatório

0

Hope this will help you out, I have tried this with all your inputs and it is working fine..

<?php

ini_set('display_errors', 1);

$startTime=10;
$endTime=20;

$startOffset=100;
$endOffset=110;

$range=range($startTime,$endTime);//getting range of time

$offsetRange=range($startOffset,$endOffset);//getting range of offset


$set=array_intersect($range, $offsetRange);//getting the intersection

if(is_array($set) && count($set)>0);
{
    if(count($set)>1)
    {
        echo "Matched";
    }
    //added this to prevent this case offerStartTime: 1, offerEndTime: 10, 
    //ranges intersection is the endTime
    elseif(count($set)==1)
    {
        if($set[0]!=$startTime)
        {
            echo "Matched";
        }
    }
}
about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda