You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP循环添加分钟值问题:如何让输出包含起始时间

解决PHP循环生成时间缺失起始时间的问题

方法一:先输出起始时间,再生成后续时间

保持原有的$slot_count表示需要生成的后续时间数量(5个),加上起始时间总共得到6个结果:

$slot_count = 5;

$start = strtotime("08:00");
// 先输出起始时间
echo date("H:i:s", $start) . '<br/>';

for( $i=0; $i<$slot_count; $i++ )
{
    $start = strtotime('+20 minutes', $start);  
    $end_new = date("H:i:s", $start);

    echo $end_new . '<br/>';
} 

方法二:直接控制总时间点数量

直接定义需要生成的总时间槽数(6个),循环内先输出当前时间再更新,逻辑更直观:

$total_slots = 6; // 对应期望的6个时间点
$start = strtotime("08:00");

for( $i=0; $i<$total_slots; $i++ )
{
    echo date("H:i:s", $start) . '<br/>';
    // 输出后再增加20分钟
    $start = strtotime('+20 minutes', $start);
} 

两种方法都能得到期望输出:

08:00:00
08:20:00
08:40:00
09:00:00
09:20:00
09:40:00

内容的提问来源于stack exchange,提问作者Rzj EM

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 12:46:58