Criteria for dynamic inputs

Asked

Viewed 40 times

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 chosen date is today and now is 16:13, my input should display

17:00 - 18:00 - 19:00 - 20:00 - 21:00 - 22:00 - 23:00

Follows 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;
}


?>

2 answers

2

You can check in your looping the current time.

//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 php’smysql_ functions are deprecated, you must use PDO or mysqli_

  • Good morning, the code works but if the chosen date is tomorrow the field still performs the filter I tried to solve here with an if more could not if($predata == $hoje) continue;, i need the filter to occur only if the chosen date is equal to today.

  • I changed the answer, just you capture the current date and on if comparing schedules, comparing the full date

  • Good afternoon, I tried to do this way but it appears all available hours even if I select today’s date.

  • You are changing the variable date $dataSelecionada?

  • Yes, I put $dataSelecionada = $predata

  • The format of this $predata this in Y-m-d ?

  • No, but I made the change, my date format is d m Y, in your code I made the adaptation strtotime(date('d\m\Y H:i'))), I also did the test by placing the date selected manually and with its format and got the same problem.

  • The strtotime only accepts date formats in English, you must leave as this in the code and change your date to the correct format

Show 3 more comments

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;
}                       
?>

Browser other questions tagged

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