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

126
Visualizações
Compare time difference inside if condition

I need to compare the time difference between "todays date and time" and two dates in the future. These dates are including time informations, too.

At the moment I'm having the problem that the "if condition" doesn't output the expected result.

That's the code and the live example:

$date_a = new DateTime('now');

$date_b2 = new DateTime('wednesday + 7 day 08:00');
$date_c2 = new DateTime('saturday + 7 day 20:00');

$intervaln = date_diff($date_a,$date_b2);
$interval2n = date_diff($date_a,$date_c2);

$diff1n = $intervaln->format('%d d. %h h. %i min.');
$diff2n = $interval2n->format('%d d. %h h. %i min.');

    if ($diff1n > $diff2n) {
      echo $diff1n;
    } 
    else if ($diff1n < $diff2n) {
       echo $diff2n;
    }

As you see it outputs the second condition but it should be the first one since $diff1n has the bigger time difference than $diff2n.

What's the right way to compare these dates and times to each other?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Try strtotime() on the times .

$diff1n = $intervaln->format('%d d. %h h. %i min.');
$diff2n = $interval2n->format('%d d. %h h. %i min.');
$d1=strtotime($diff1n);
$d2=strtotime($diff2n);
if ($d1> $d2) {
  echo $diff1n;
} 
else if ($d1< $d2) {
   echo $diff2n;
}
over 4 years ago · Santiago Trujillo Relatório

0

You can convert the dates to timestamp using the setTimestamp() function, then get the difference between the two, divide it by no of seconds that make a day round of to .2 float and print it like "2 days from now" then compare it to the no. of days i.e

// $diffdateAandNowtime is the difference between the uni time of dateA and nowtime

// $diffdateBandNowtime is the difference between the uni time of dateB and nowtime

    $diffdateAandNowtime  = 4600; 
    $diffdateBandNowtime  = 5600; 
    
    $nowtime = 36000;
    
    //time here is in seconds
    
        if( $diffdateAandNowtime > $diffdateBandNowtime )
        {
        //run some code here
     echo $diffdateAandNowtime;
 
        }
        elseif( $diffdateBandNowtime > $diffdateAandNowtime )
        {
    
    echo $diffdateAandNowtime;

        }

refer here for setTimestamp()

over 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