【发布时间】:2017-11-11 16:14:19
【问题描述】:
我有在两次之间获取时间的功能,我希望这个代码像这样打印
数组([0] => 07:00:00 [1] => 08:00:00 [2] => 09:00:00 [3] => 10:00:00 [4] => 11:00:00)
(第一个元素未打印/添加)
下面的代码打印如下。
数组([0] => 06:00:00 [1] => 07:00:00 [2] => 08:00:00 [3] => 09:00:00 [4] => 10:00:00 [5] => 11:00:00)
代码
$si="06:00 AM";
$sb="11:00 AM";
$st= date ( 'H:i:s', strtotime ($si) );
$en=date( 'H:i:s', strtotime ($sb ) );
$NoOfHours = $this->getTimesfromRange(date('H:i:s', strtotime($st)),date('H:i:s',strtotime($sb)));
print_r($NoOfHours);
函数获取时间
public function getTimesfromRange($start, $end){
$dates = array($start);
while(end($dates) < $end){
if(date('H:i:s', strtotime(end($dates).' +1 hour'))==$start){
continue;
}else{
$dates[] = date('H:i:s', strtotime(end($dates).' +1 hour'));
}
}
return $dates;
}
问题:如何在 while 循环中不打印第一个元素,我尝试使用 continue 但不起作用。
【问题讨论】:
-
continue是正确的。因此,如果这不起作用,则意味着您的 if 语句实际上是错误的 -
改为 for 循环并使用 if($i != 0){ }
-
@TomaszAdamczyk 仍在尝试其中一些,我的本地主机似乎很慢,Andreas 谢谢我试试
-
@Muklas 我添加了一个答案,但我不确定它是否正确。你有 $end,不确定它的用途以及如何将它放入代码中。我回答了从 1-> 数组末尾循环
标签: php arrays loops time while-loop