简体   繁体   中英

Skip a value in do-while loop

I've got a script that sets up a array of dates which form a dropdown. The array is set up by some variables about the current date and an fixed offset for the last date (two weeks). Stripped down this is what it looks like:

public function getDatesOptionArray()
{
    $datesArray = array();
    $displayDate = Mage::getModel('core/locale')->storeDate();
    $displayDate->add($this->_startDaysOffset, Zend_Date::DAY);
    $dayOffset = $this->_startDaysOffset;

    do
    {
        $dayofweek = date('w', strtotime($displayDate->toString(Varien_Date::DATE_INTERNAL_FORMAT)));

        $datesArray[$displayDate->toString(Varien_Date::DATE_INTERNAL_FORMAT)] = Mage::helper('core')->formatDate($displayDate, Mage_Core_Model_Locale::FORMAT_TYPE_FULL);
        $displayDate->add('1', Zend_Date::DAY);
        $dayOffset++;

    } while ($dayOffset <= $this->_endDaysOffset);

    return $datesArray;
}

The thing is I want to leave out all the 'Sunday' options, and I've got the $dayofweek variable for each, where sunday is 0. I've tried to wrap the whole thing inside the do function in an if-statement ( if $dayofweek !== 0 ), set an if ($dayofweek == 0) { continue;} and every other trick I could think of, but I get only either of these results

  • 1: Only the first date is shown
  • 2: All dates until the first sunday are shown, none after that
  • 3: All dates are shown

I think I might be missing the point on the do-while loop; how do I exclude if $dayofweek == 0 ?

For me is something like this, i use while because Do...While(...) the first time it will not check your condition you'll enter your loop at last 1 time, and when you use while(...){} every time your program will check your condition

public function getDatesOptionArray()
{
  $datesArray = array();
  $displayDate = Mage::getModel('core/locale')->storeDate();
  $displayDate->add($this->_startDaysOffset, Zend_Date::DAY);
  $dayOffset = $this->_startDaysOffset;


  while ($dayOffset <= $this->_endDaysOffset)
  {
   $dayofweek = date('w', strtotime($displayDate->toString(Varien_Date::DATE_INTERNAL_FORMAT)));
   if ($dayofweek != 0) {
      $datesArray[$displayDate->toString(Varien_Date::DATE_INTERNAL_FORMAT)] = Mage::helper('core')->formatDate($displayDate, Mage_Core_Model_Locale::FORMAT_TYPE_FULL);
    }
    $displayDate->add('1', Zend_Date::DAY);

    $dayOffset++;
  }
  return $datesArray;
}

Let say the array is

$date = array(
    2017-10-12,
    2017-10-13,
    2017-10-14,
    2017-10-15,
    2017-10-16,
    2017-10-17,
    2017-10-18,
    2017-10-19,
    2017-10-20,
    2017-10-21,
    2017-10-22,
    2017-10-23,
    2017-10-24,
    2017-10-25
);

Now do this for remove sunday's dates

$i=0;
do{
    if(date('w',strtotime($date[$i]))>0) $dateArray[] = $date[$i];
    $i++;
} while ($i<count($date));
echo "<pre>";print_r($dateArray);
return $datesArray;

$dateArray will give you dates without sundays.

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