Highlight a time with PHP that is stored in the database

Asked

Viewed 123 times

2

I am creating a small table that has 7 columns, the first row of each column represents a day and the remaining are times of that day.

I want the times that are stored in the bank to be highlighted in this table. I tried a few ways, I didn’t succeed and I’d like a little help.

Here’s the code, and I apologize for any basic mistake, I’m still not very familiar with PHP.

<table border="1">      
<?php
    $query = array();
    echo '<tr>';
    for ($i = 1; $i <= 7; $i++) {
        $data = date('d-m-Y',strtotime('+' . $i . ' days'));
        $query[$i] = mysql_query ("SELECT hora_consulta FROM consulta WHERE data_consulta =  '$data'");
        echo '<td>' . $data . '</td>';
    }
    echo '</tr>';

    $horario = '08:00';

    while (strtotime($horario) < strtotime('20:00')) {
        echo '<tr>';
        echo '<td>' . $horario . '</td>' .
             '<td>' . $horario . '</td>' .
             '<td>' . $horario . '</td>' .
             '<td>' . $horario . '</td>' .
             '<td>' . $horario . '</td>' .
             '<td>' . $horario . '</td>' .
             '<td>' . $horario . '</td>' ;

        $horario = strtotime("$horario + 30 minutes");
        $horario = date("H:i",$horario);
        echo '</tr>';
    }

?>
</table>

2 answers

1


I think that’s what you want:

/*
* Inseri datas e horas no banco (apaguei algumas para simulação)

for ($i = 0; $i < 10; $i++) {
    $data = date("Y-m-d H:i:s", strtotime("today + $i days 08:00:00"));
    for ($a = 0; $a <= 8; $a++) {
    $data = date("Y-m-d H:i:s", strtotime("$data + 1 hours"));
    mysql_query("INSERT INTO dias_horas (data_hora) VALUES ('$data')") or die(mysql_error());
}
}*/

    echo "<table border=\"1\">";

$data_inicial = $data_loop = date("Y-m-d"); //Data de hoje 
$data_final = date("Y-m-d", strtotime("$data_inicial + 10 days"));

$data_aux = array();//Array que armazenará as datas e horas do banco de dados

$busca = mysql_query("SELECT data_hora FROM dias_horas ORDER BY data_hora");
while ($data = mysql_fetch_assoc($busca)) {
    $data_aux[] = date("Y-m-d H:i:s", strtotime($data["data_hora"]));
}


echo "<tr>";
while ($data_loop <= $data_final) { //O loop para o intervalo de dias
    echo "<th>" . date("d/m/Y", strtotime($data_loop)) . "</th>";
    $data_loop = date("Y-m-d", strtotime($data_loop . " + 1 days"));
}
echo "</tr>";


for ($i = 0; $i < 24; $i++) { //Loop para as horas do dia
    $data_loop = $data_inicial;
    echo "<tr>"; //Cada hora é em uma linha
    while ($data_loop <= $data_final) {
        echo "<td";
        /*Se houver aquela data e hora que o loop está percorrendo
        dentro do array, imprime o estilo negrito
        */
        echo in_array(date("Y-m-d H:i:s", strtotime($data_loop . " $i:00:00")), $data_aux) ? " style='font-weight:bold'" : "";
        echo ">$i:00</td>";
    $data_loop = date("Y-m-d", strtotime($data_loop . " + 1 days"));
    }
    echo "</tr>";
}

echo "</table>";
?>`

  • Exactly what I needed, thank you so much for your help!

0

Hello. from what I understand. Do you want to highlight your schedule with a different color? If yes. You can add a tag html, example: <red> or a style color in css

echo "<td>".$horario."</td>
      <td><red>".$horario."</red></td>
      <td style='color:#FFFFFF'>".$horario."</td>
      <td><s>".$horario."</s></td>
      <td><b>".$horario."</b></td>
      <td><red>".$horario."</red></td>
      <td><blue>".$horario."</blue></td>" ;
  • That, but I would like to do according to some data in the bank, but thank you for the reply!

Browser other questions tagged

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