Whire between start and end time with 30-minute multiples

Asked

Viewed 98 times

0

Hello. How to make a While within an hour initial / final and in 30-minute multiples.

Example:

<option value="08:00">08:00</option>
<option value="08:30">08:30</option>
<option value="09:00">09:00</option>
<option value="09:30">09:30</option>
.
.
.
<option value="20:00">20:00</option>

My code

<select name="pick-up-time" id="pick-up-time">
  <?php 
    $horaInicial = "08:00";
    $horaFinal = "20:00";

    do{ 
  ?>
    <option value="00:00">00:00</option>
  <?php } while ($row_rsLoja = $rsLoja->fetch_assoc()); ?>
</select>
  • I didn’t get it right, you want to generate an option with value of 30 mintuos per 24h?

  • I need to make the loop picking the initial hour until the final, only displaying every 30 minutes, as in the example.

1 answer

-1

For those who have the same question, I found a site that had a similar model, so I just adapted it to what I wanted.

Pity that the code is great...rsrs, I believe that a person with knowledge manages to reduce to a few lines...

<select name="pick-up-time" id="pick-up-time">
<?php
    $timeFormat = "%02d:%02d";
    $startHour = "06:00";
    $endHour = "22:00";
    $timeInterval = 30; // in minutes

    $currentHour = $startHour;
    $currentMinute = 0;
    while ($currentHour <= $endHour) {
        $timeRange = sprintf($timeFormat, $currentHour, $currentMinute);

        $currentMinute = $currentMinute + $timeInterval;
        if ($currentMinute > 59) {
            $currentHour++;
            $currentMinute = $currentMinute - 60;
        }

        if ($currentHour <= $endHour) {
            echo "<option value=\"$timeRange\">$timeRange</option>";
        }       
    }

?>
</select>

Browser other questions tagged

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