Kind of new to this, so sorry if I'm asking a dumb question.
I have made a list of times, which I have to put into a select menu. Kinda like this .
I actually want my list to be from 16:00 until 01:45 or 02:00 instead of 00:00-01:45 & 16:00-23:45, but I don't know how. And then the second thing is I couldn't manage to get this list into a
<select>
dropdown menu
This is the code I've got so far:
<?php
$exclude = array(2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
function echo_datelist($i, $j)
{
$time = str_pad($i, 2, '0', STR_PAD_LEFT).':'.str_pad($j, 2, '0', STR_PAD_LEFT);
echo $time.'<br />';
}
for ($i = 00; $i <= 23; $i++)
{
for ($j = 0; $j <= 45; $j+=15)
{
if (in_array($i, $exclude)) continue;
echo_datelist($i, $j);
}
}
?>
If you want to start at 16:00 and go to 1:45 you could use a modulus like so inside your first for loop $t = ($i + 16) % 24
and then use this variable instead of $i inside you other loop. If you want to put it in a select element you need to surround your loop with <select>
</select>
here is an example of what you are looking for.
<?php
$exclude = array(2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
function echo_datelist($i, $j)
{
$time = str_pad($i, 2, '0', STR_PAD_LEFT).':'.str_pad($j, 2, '0', STR_PAD_LEFT);
echo "<option value=" .$time. ">" . $time . "</option>"; // print in option
}
echo "<select>"; // this to enclose it all in a select element
for ($i = 00; $i <= 23; $i++){
$hour = ($i + 16) % 24; // this to start at 16:00 and wrap around to 1:45
for ($j = 0; $j <= 45; $j+=15)
{
if (in_array($hour, $exclude)) continue;
echo_datelist($hour, $j);
}
}
echo "</select>";
You can use mktime() which will generate the time you want with date() to handle the key and the value for your dropdown.
So, for example,
$fromTime = mktime(16, 0, 0, 11, 8, 2017); // (11-08-2017 16:00:00)
$toTime = mktime(2, 0, 0, 11, 9, 2017); // (11-09-2017 02:00:00)
$i = 15; // every 15 mins
$timePointer = $fromTime;
while($timePointer <= $toTime) {
$timesList[$timePointer] = date('H:i', $timePointer);
$timePointer += $i * 60; // mult by 60 to convert to minutes
}
print_r($timesList);