[英]PHP how to skip first element in while loop
i have function to get time between two time, i want this code print like this 我有获取两个时间之间的时间的功能,我想要这样的代码打印
Array ( [0] => 07:00:00 [1] => 08:00:00 [2] => 09:00:00 [3] => 10:00:00 [4] => 11:00:00 )
数组([0] => 07:00:00 [1] => 08:00:00 [2] => 09:00:00 [3] => 10:00:00 [4] => 11:00 :00)
(first element not printed/added) (第一个元素未打印/添加)
this below code print like this. 下面的代码是这样打印的。
Array ( [0] => 06:00:00 [1] => 07:00:00 [2] => 08:00:00 [3] => 09:00:00 [4] => 10:00:00 [5] => 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)
the code 编码
$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);
function get time 功能获取时间
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;
}
Question : how to not print first element in while loop, i tried use continue but not working. 问题:如何不打印while循环中的第一个元素,我尝试使用continue但不能正常工作。
The array_shift() function removes the first element from an array, and returns the value of the removed element. array_shift()函数从数组中删除第一个元素,并返回已删除元素的值。 You can change your function as below to work.
您可以按以下方式更改功能以工作。 You can find more details here
您可以在这里找到更多详细信息
public function getTimesfromRange($start, $end){
$dates = array($start);
while(end($dates) < $end){
$dates[] = date('H:i:s', strtotime(end($dates).' +1 hour'));
}
array_shift($dates);
return $dates;
}
Just change this code $dates = array($start);
只需更改此代码
$dates = array($start);
to $dates = [];
到
$dates = [];
which will not store the $start to your array. 不会将$ start存储到您的数组中。 And you have to modify your function like this, Live demo .
并且您必须像这样修改您的功能, Live demo 。
function getTimesfromRange($start, $end){
$dates = [];
while(end($dates) < $end){
$date = end($dates) != FALSE ? end($dates) : $start;
$dates[] = date('H:i:s', strtotime($date.' +1 hour'));
}
return $dates;
}
You also can remove the first element from the result array, with 您还可以使用以下方法从结果数组中删除第一个元素
array_shift($result);
or unset($result[0]);
或未
unset($result[0]);
or array_slice($result, 1);
或
array_slice($result, 1);
. 。 These are not recormended.
这些不建议。
If I understand your question, you're getting the correct dates in your array but you want to print everything except element 0. 如果我理解您的问题,则可以在数组中获得正确的日期,但您希望打印除元素0以外的所有内容。
Personally I would simply copy it and remove element 0. 就我个人而言,我只是将其复制并删除元素0。
$NoOfHours = $this->getTimesfromRange(date('H:i:s', strtotime($st)),date('H:i:s',strtotime($sb)));
$printable = $NoOfHours;
unset($printable[0]);
var_dump($printable);
Assuming it's the first value that you don't want to display regardless of the time you could use a counter $i;
假定它是您不希望使用计数器
$i;
的任何时间显示的第一个值$i;
public function getTimesfromRange($start, $end){
$dates = array($start);
$i = 0;
while(end($dates) < $end){
if($i != 0){
$dates[] = date('H:i:s', strtotime(end($dates).' +1 hour'));
}
$i++;
}
return $dates;
}
For loop to exclude [0] For循环排除[0]
public function getTimesfromRange($start, $end){
$dates = array($start);
For($i=0; $i<count($dates)); $i++){
If($i != 0){
$dates[] = date('H:i:s', strtotime(end($dates).' +1 hour'));
}
}
return $dates;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.