简体   繁体   中英

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 )

(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 )

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.

The array_shift() function removes the first element from an array, and returns the value of the removed element. 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); to $dates = []; which will not store the $start to your array. And you have to modify your function like this, 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]); or 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.

Personally I would simply copy it and remove element 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;

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]

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;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM