0

I can get every monday and tuesday but every weeks

How can I get to every 2 weeks

$days = [1,2];
$filterDayOfWeek = function ($date) use ($days) {
        return in_array($date->dayOfWeekIso, $days, true);
};

$period = Carbon::parse($this->entry_start->toDateString())->daysUntil(
        $this->periodicity_end->toDateString()
    );
$period->addFilter($filterDayOfWeek);

I saw in the documentation this:

 $interval = \Carbon\CarbonInterval::weeks(2);

But I don't know how to apply to my perdiod

Thank you

  • I don't think an interval will help here. Instead, see if the week of the year number is divisible by 2. – aynber Aug 13 at 14:38
  • Yes I have think about that, but in my application it can be every 3, 4, 5 weeks... – Jsenechal Aug 13 at 15:17
  • 1
    You can use the same logic, just change the modulus number. $date->weekOfYear % 4 == 0 – aynber Aug 13 at 15:21
  • We need precisions: for example if entry_start is Tuesday, will the skipped weeks be between Monday and Tuesday, or will they stick together. What can contain entry_start and how to determine the first date? What can contains $days? Please some examples, so we could give you solution that fit your use case. – KyleK Aug 15 at 8:05
  • Question: Why Carbon::parse($this->entry_start->toDateString()) since $this->entry_start is already a Carbon instance? – KyleK Aug 15 at 8:08
0

In every 2 weeks, you first have to determine how you filter odd/even or from a point of start.

CarbonInterval::weeks(2) can be used only if nested iterations: 1 loop for 2-weeks, then a loop for days:

$days = ['Monday', 'Tuesday'];
foreach (CarbonPeriod::create($start, CarbonInterval::weeks(2), $end, CarbonPeriod::IMMUTABLE) as $baseDate) {
  foreach ($days as $dayName) {
    $date = $baseDate->is($dayName) ? $baseDate : $baseDate->next($dayName);
    echo $date;
  }
}

See my comment, if you can give more precision. I can take it into account in the answer.

0

Here is my solution

function forEveryWeek(Periodicity $periodicity): CarbonPeriod
{
    /**
     * monday, tuesday, wednesday
     * @example : [1,2,3]
     */
    $days = $periodicity->getDays();
    /**
     * @example 1 for every weeks, 2 every 2 weeks, 3,4...
     */
    $repeat_week = $periodicity->getRepeatWeek();

    /**
     * filter days of the week
     * @param $date
     * @return bool
     */
    $filterDayOfWeek = function ($date) use ($days) {
        return in_array($date->dayOfWeekIso, $days, true);
    };

    /**
     * Carbon::class
     * $this->entry_start
     * $this->periodicity_end
     */
    $period = Carbon::parse($this->entry_start->toDateString())->daysUntil(
        $this->periodicity_end->toDateString()
    );

    /**
     * filter every x weeks
     * @param $date
     * @return bool
     */
    $filterWeek = function ($date) use($repeat_week) {
        return $date->weekOfYear % $repeat_week === 0;
    };

    $period->excludeStartDate();
    $period->addFilter($filterDayOfWeek);

    if ($repeat_week > 1) {
        $period->addFilter($filterWeek);
    }

    return $period;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.