Php 需要以1小时为增量显示时间列表的帮助吗
我写了一些代码,可以输出从早上6:00到晚上23:45的时间列表 我正在使用DateTime类来完成此任务。我遇到的问题是,while循环是一个无限while循环。它不会停止 我真的很难理解为什么当我将条件设置为此时它不会停止<代码>while($格式化\u开始\u时间设置时间(6,00); $formatted_start_time=$start_hour->format(“H:i:s”); $end_hour=mktime(23,45); 而($U开始时间修改(“+60分钟”); $formatted_start_time=$start_hour->format(“H:i:s”); echo$格式化\u开始\u时间; }Php 需要以1小时为增量显示时间列表的帮助吗,php,date,datetime,Php,Date,Datetime,我写了一些代码,可以输出从早上6:00到晚上23:45的时间列表 我正在使用DateTime类来完成此任务。我遇到的问题是,while循环是一个无限while循环。它不会停止 我真的很难理解为什么当我将条件设置为此时它不会停止while($格式化\u开始\u时间设置时间(6,00); $formatted_start_time=$start_hour->format(“H:i:s”); $end_hour=mktime(23,45); 而($U开始时间修改(“+60分钟”); $formatte
这很简单,可读,使用方便:
您正在比较两个非常不同的变量:
$formatted\u start\u time=='06:00:00'
而$end\u hour==1408941938'
(或类似)。啊。好的。我明白您的意思。这很有意义。
$start_hour = new DateTime("now",new DateTimeZone("America/New_York"));
$start_hour->setTime(6,00);
$formatted_start_time = $start_hour->format("H:i:s");
$end_hour = mktime(23,45);
while ($formatted_start_time <= $end_hour){
$start_hour->modify("+60 minutes");
$formatted_start_time = $start_hour->format("H:i:s");
echo $formatted_start_time;
}
$start_hour = new DateTime('06:00', new DateTimeZone("America/New_York"));
// Add one minute as the loop is NOT inclusive
$end_hour = new DateTime('23:46', new DateTimeZone("America/New_York"));
$interval = new DateInterval('PT15M');
$period = new DatePeriod($start_hour, $interval, $end_hour);
foreach ($period as $time) {
echo $time->format('H:i:s');
}