I can not compare dates

0

Can anyone tell me what's happening in this code? The first querie works however it does not go into condition within the for. The second works and behaves normally, no problem with this. The case is that I have never seen this before my comparison inside for it never gets true even though they are both of the same length and of the same type.

follow the code:

public function getDias($dia){
    $sql=$this->conn->query("SELECT * FROM agendamento WHERE dia = '$dia'");
    // $sql=$this->conn->query("SELECT * FROM agendamento");
    $horaBD = array(8=>"1");
    $responseArr = [];
    while($row = $sql->fetch_assoc()){     
      array_push($horaBD,date("H:i",strtotime($row['horario'])));
    }

    for ($i=9; $i <= 18; $i++) {
      $horaFor = ($i<10) ? "0".$i.":00" : $i.":00";
      if ($horaFor == @$horaBD[$i]) continue;//aqui eu substitui por and para funcionar do jeito esperado
      else array_push($responseArr,$horaFor);
    }
    echo "<br>". json_encode($responseArr);

  }
I forgot to mention that I'm using version 7.2 of php I do not know if that matters.

as read by LipESprY:

horaFor : 09:00   horaDB: 10:00
horaFor : 10:00   horaDB: 11:00
horaFor : 11:00   horaDB: 15:00
horaFor : 12:00   horaDB: 11:00
horaFor : 13:00   horaDB: 15:00
    
asked by anonymous 09.12.2018 / 02:03

1 answer

0

public function getDias($dia,$hralmoco=12,$intervalo=1){

    $dia = date("Y-m-d",strtotime($dia));
    $sql=$this->conn->query("SELECT * FROM agendamento WHERE dia = '$dia'");
    $horaBD = array(8=>"1");
    $horaFor = array(8=>"1");
    $responseArr=array();
    while($row = $sql->fetch_assoc()){
      array_push($horaBD,date("H:i",strtotime($row['horario'])));
    }
    for($i=9;$i<=18;$i++){
      if($i >= $hralmoco && $i < $hralmoco+$intervalo) continue;

      ($i<10) ? array_push($horaFor,("0".$i.":00")):array_push($horaFor,($i.":00"));
    }
    unset($horaBD[8],$horaFor[8]);
    //essa foi a solução encontrada pois eu tinha que entrar no array horaBD
    //para apagar o indice em que o valor do Array HoraFor fosse igual a horaBD
    // não sei deu para entender bem :(

    //Solução
    foreach($horaBD as $key => $value){
      for ($i=9; $i < 19; $i++) {
        if($value == @$horaFor[$i]) {
          unset($horaFor[$i]);
        }
      }
    }
     //Solução
    forEach($horaFor as $key=>$value){
      array_push($responseArr);
    }

    echo json_encode($responseArr);
  }

After running the above code, return these arrays $ responseArr is subtraction of $ hourDB and $ hourFor where it returns only the difference between them

array $ timeBD {

 [9] => 10:00
[10] => 11:00
[11] => 15:00
[12] => 11:00
[13] => 15:00

}

array $ timeFor {

 [9] => 09:00
[10] => 10:00
[11] => 11:00
[12] => 13:00
[13] => 14:00
[14] => 15:00
[15] => 16:00
[16] => 17:00
[17] => 18:00

}

$ responseArr = {

[0] => 09:00
[1] => 13:00
[2] => 14:00
[3] => 16:00
[4] => 17:00
[5] => 18:00

}

The problem was that before my was being compared like this:

for ($i=9; $i <= 18; $i++) {
      $horaFor = ($i<10) ? "0".$i.":00" : $i.":00";
      if ($horaFor == @$horaBD[$i]) continue;//aqui eu substitui por and para funcionar do jeito esperado
      else array_push($responseArr,$horaFor);
    }

and this was generating the following code:

horaFor : 09:00   horaDB: 10:00
horaFor : 10:00   horaDB: 11:00
horaFor : 11:00   horaDB: 15:00
horaFor : 12:00   horaDB: 11:00
horaFor : 13:00   horaDB: 15:00

ie I was not comparing all the range values, within $ hourBD with the $ hourFor, I was only with compared them directly, like this:

$horaFor = 10;
$horaDB = 12;
if($horaDB == $horaFor) continue;

To solve this I use updated code block:

foreach($horaBD as $key => $value){
      for ($i=9; $i < 19; $i++) {
        if($value == @$horaFor[$i]) {
          unset($horaFor[$i]);
        }
      }
    }

Now it reads the entire value inversion within $ hourDB and if there is any match it deletes it from the array $ responseArr;

    
09.12.2018 / 21:05