Criteria on dynamic inputs

2

I have a dynamic input that checks the date the user selects and then returns the available hours for scheduling, however when the user selects the date of the day (data = hoje) I would like the hours that have passed to be deleted from the input.

Example:

The date chosen is today and it is now 4:13 pm, my input should display

17:00 - 18:00 - 19:00 - 8:00 pm - 21:00 - 22:00 - 23:00

Follow the code:

<?php
session_start();
require_once('library.php');
$hoje = date('d/m/Y');
$amanha = date("d/m/Y", mktime(0,0,0,date("m"),(date("d")+1),date("Y"))); 
$damanha = date("d/m/Y", mktime(0,0,0,date("m"),(date("d")+2),date("Y")));



$predata = $_POST['predata'];

$sql = "SELECT Hora FROM tbl_agenda WHERE Data = '$predata'";
$atualizahoje = "UPDATE tbl_agenda SET Data='". $hoje ."' WHERE id=33";
$atualizaamanha = "UPDATE tbl_agenda SET Data='". $amanha ."' WHERE id=34";
$atualizadamanha  = "UPDATE tbl_agenda SET Data='". $damanha ."' WHERE id=35";
$qr1 = mysql_query($atualizahoje) or die(mysql_error());
$qr2 = mysql_query($atualizaamanha) or die(mysql_error());
$qr3 = mysql_query($atualizadamanha) or die(mysql_error());
$qr = mysql_query($sql) or die(mysql_error());
$ln1 = mysql_fetch_assoc($qr1);
$ln2 = mysql_fetch_assoc($qr2);
$ln3 = mysql_fetch_assoc($qr3);

$horaatual = date('H:00');

                $horas_hidden = array();
while ($ln = mysql_fetch_assoc($qr)) {
    $horas_hidden[] = $ln['Hora'];
}
echo montarOptions($horas_hidden, 23);

function montarOptions($horas_hidden, $total_horas)
{

    $html='<option value="">Selecione..</option>';



    for ($i=0; $i <= $total_horas; $i++) {
        $hora = ($i < 10) ? '0'.$i : $i;
        $hora .=':00';


        if (in_array($hora, $horas_hidden)) {
          $html.= "<option hidden value=\"{$hora}\">{$hora}</option>";
        } else {
          $html.= "<option value=\"{$hora}\">{$hora}</option>";
        }





   }
   return $html;
}


?>
    
asked by anonymous 28.06.2016 / 21:15

2 answers

0

I was able to solve the problem as follows:

$horaatual = date('H:00');
$horas_hidden = array();
echo $hoje;



while ($ln = mysql_fetch_assoc($qr)) {
    $horas_hidden[] = $ln['Hora'];
}

if ($predata == $hoje) {
echo montarOutro($horas_hidden, 23);
}
else {
echo montarOptions($horas_hidden, 23);
}

function montarOptions($horas_hidden, $total_horas)
{
 $html.='<option value="">Selecione...</option>';





for ($i=0; $i <= $total_horas; $i++) {

    $hora = ($i < 10) ? '0'.$i : $i;
    $hora .=':00';

    //S¨® deixa passar o restante do c¨®digo se o $i for maior ou igual que a hora atual

    if(strtotime($dataSelecionada.' '.$hora) < strtotime(date('d/m/Y H:i'))) continue;


    if (in_array($hora, $horas_hidden)) {
      $html.= "<option hidden value=\"{$hora}\">{$hora}</option>";
    } else {

        $html.= "<option value=\"{$hora}\">{$hora}</option>";

    }


}
   return $html;
}



function montarOutro($horas_hidden, $total_horas)
{
 $html.='<option value="">Selecione...</option>';
$dataSelecionada = $predata;

for ($i=0; $i <= $total_horas; $i++) {

    $hora = ($i < 10) ? '0'.$i : $i;
    $hora .=':00';

    //Só deixa passar o restante do código se o $i for maior ou igual que a hora atual
    if(strtotime($dataSelecionada.' '.$hora) < strtotime(date('Y-m-d H:i'))) continue;

    if (in_array($hora, $horas_hidden)) {
      $html.= "<option hidden value=\"{$hora}\">{$hora}</option>";
    } else {
      $html.= "<option value=\"{$hora}\">{$hora}</option>";
    }
}
   return $html;
}                       
?>
    
16.07.2016 / 15:16
2

You can check the current time in your looping.

//Captura a data selecionada
$dataSelecionada = '2016-06-30';

for ($i=0; $i <= $total_horas; $i++) {

    $hora = ($i < 10) ? '0'.$i : $i;
    $hora .=':00';

    //Só deixa passar o restante do código se o $i for maior ou igual que a hora atual
    if(strtotime($dataSelecionada.' '.$hora) < strtotime(date('Y-m-d H:i'))) continue;

    if (in_array($hora, $horas_hidden)) {
      $html.= "<option hidden value=\"{$hora}\">{$hora}</option>";
    } else {
      $html.= "<option value=\"{$hora}\">{$hora}</option>";
    }
}

Note: The mysql_ functions of php are deprecated, you should use PDO or mysqli _

    
28.06.2016 / 21:28