incrementing time 15 minutes within loop

后端 未结 2 1494
故里飘歌
故里飘歌 2021-01-26 13:21

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 minute

相关标签:
2条回答
  • 2021-01-26 13:56

    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');
    }
    
    0 讨论(0)
  • 2021-01-26 13:57

    there are a couple of things. First on your first line you are missing the second parameter. Then you are using $tNow undefined.

    $current_time = date('h:i A', time());
    $end_hour     = date("+24 hours", strtotime($current_time));
    
    
    $tNow = strtotime($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', $tNow);
    }
    
    0 讨论(0)
提交回复
热议问题