douzhang3822 2016-11-24 07:15
浏览 70
已采纳

循环内增加时间15分钟

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.

  • 写回答

2条回答 默认 最新

  • doujiang1001 2016-11-24 07:40
    关注

    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');
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。
  • ¥15 livecharts wpf piechart 属性
  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了