BETWEEN PHP compare dates

Asked

Viewed 232 times

0

I’m making an online schedule and I need to check the schedules that are already scheduled so as not to conflict with the schedule to schedule.

I’m using PHP’s BETWEEN to compare times, but it’s not working.

Example

Time to be scheduled

inicio: 2019-03-11 09:30:00
Final: 2019-03-11 09:45:00

In the bank I have registered:

inicio: 2019-03-11 09:00:00
Final: 2019-03-11 10:00:00

I select the bank to be able to check the schedule to be scheduled:

$valor_start= $hoje->format('Y-m-d '.trim($_POST['hora_inicio']).":00".'') ;
$valor_end= $hoje->format('Y-m-d '.trim($_POST['hora_final']).":00".'');

$sql="SELECT * FROM `events` WHERE `start` BETWEEN '".implode('-', array_reverse(explode('/', substr($valor_start, 0, 10)))).substr($valor_start, 10)."' AND '".implode('-', array_reverse(explode('/', substr($valor_end, 0, 10)))).substr($valor_end, 10)."' and ativo='sim' and atendente='".$_POST['atendente']."'";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {

        ?>

<div class="col-md-6 ">
<div class="borda_data_erro">
<p><b><strong>Esse dia e horário já existe em Consultas abertas</strong> </b></p>
<p><i class="m-r-10 mdi mdi-calendar-remove"></i> Início: <b><? echo $valor_start ."-".$row['start']?></b></p>
<p><i class="m-r-10 mdi mdi-calendar-remove"></i> Final: <b><? echo $valor_end ."-".$row['end']?></b></p>
</div>      
</div>      
        <?

        $checar_horarios=$row['start'];

}}

Should return the error This day and time already exists in Open Consultations but it doesn’t happen, as it could solve?

  • What is the result of this $result wheel one var_dump($result) and tell me the result ?

  • The date field is as varchar? pq this conversion in format?

  • why it comes from the form so 11/03/2019

  • @Bulfaitelo the var_dump($result) returns this Object(mysqli_result)#6 (5) { ["current_field"]=> int(0) ["field_count"]=> int(10) ["lengths"]=> NULL ["num_rows"]=> int(0) ["type"]=> int(0) }

  • Wheel one var_dump($sql)

  • returns this string(124) "SELECT * FROM events WHERE start BETWEEN '2019-03-11 9:30:00' AND '2019-03-11 9:45:00' and active='yes' and attendant='2'"

  • To enlighten you, BETWEEN is an SQL operator, not PHP.

Show 2 more comments

1 answer

1


You are doing a select searching in the table where start is between 2019-03-11 9:30:00 and 2019-03-11 9:45:00, and from what you said, the start time that is registered at the bank is 2019-03-11 09:00:00, that is, your select will not return anything because 2019-03-11 09:00:00 is not among the values that were passed by your select, so it will not enter within the if and consequently will not return the error Esse dia e horário já existe em Consultas abertas. That is, your code is correct, you are just confused with the values in select.

Browser other questions tagged

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