I can’t compare dates

Asked

Viewed 70 times

0

Can someone tell me what’s going on in this code? The first querie works but it does not enter the condition within the for. The Second works and behaves normally, no problem with that. The case is that I’ve never seen it before my comparison inside the for it never gets true even though they both have the same length and being the same type.

follows 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 am using version 7.2 of php I don’t know if it matters.

as recorded 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

  • Try to rotate a var_dump() the two variables and visually compare the variables...

  • Thanks could see what I was doing wrong now I’m thinking of a solution. I will update the post to be more visible.

  • 1

    @Lipespry I managed to make the comparison found out what was Thanks by the tip of var_dump().

  • Fmz, Bro! Now answer your question and accept as solution.

  • @Lipespry Do I even answer my question? can it Arnaldo? kkkk

  • That’s right. Community recommendations. Keep your question from wandering around with no solution...

  • Boy. It would not be simpler to select the times between 9 and 18 hours straight in the query. Its a mess your code. SELECT * FROM scheduling WHERE dia = '$dia' AND TIME(time) BETWEEN '12:00:00' AND '18:00:00'

  • @Marcosxavier I don’t think it would work because I need the times that are already scheduled between 12:00 and 18:00 to be subtracted from the final array showing only the available times, thanks for the tip. recognize that my code is messy, :) but I’m trying to improve, before nor did the object-oriented code kkkk,

Show 3 more comments

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 executing the above Code return these arrays $responseArr is subtraction of $horaDB and $horaFor where it returns only the difference between them

array $horaBD{

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

}

array $horaFor{

 [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 mine was being compared so:

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

that is I was not comparing all the range values, within $horaBD with $horaFor, I was just comparing them directly, thus :

$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 all value inverting within $horaDB and if there is any match it eliminates from the $responseArr array;

  • He failed to explain "what was the solution" and mark the answer as a solution. ;)

  • @Lipespry updated the answer, I think now you can understand what I was trying to do =), I’m sorry I didn’t formulate the question in an understandable way .

  • What a fight, eh! kkkk

  • 1

    @Lipespry Thanks for the help I wasn’t even thinking about the var_dump() at the time, I was almost giving up.

  • Then try it print_r($variavel);. It has similar effect, but is more understandable. Let’s say it does the same thing, without saying the type of each branch of the variable/vector...

  • @Lipespry Really helps this print_r, thanks for the tips.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.