Mark Alan Mark Alan - 1 year ago 79
PHP Question

incrementing time 15 minutes within loop

I want to create a dropdown that will take current time as start time and will end up till 24 hours, like till coming 24 hours so in between it will show time of every 15 minutes increment. The problem is that when I try to run the loop starting time is okay but with next loop the time jumps to 6 hours behind.

Here is my code:

$current_time = date('h:i A');
$end_hour = date("+24 hours", $current_time);

echo "<option>" . $current_time . "</option>";
for($i = 0; $i <= 96; $i++) {
echo "<option>" . date("h:i A", $tNow) . "</option>";
$tNow = strtotime('+15 minutes',$current_time);

Output coming as
11:08 PM
4:00 PM
4:15 PM
4:30 PM

And so on.

Answer Source

You can use DateTime for that:

$now = new DateTime();
$end = clone $now;
$end->modify("+24 hours");

while ($now <= $end) {
    echo "<option>" . $now->format('h:i A'). "</option>";
    $now->modify('+15 minutes');
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download