title: Calculate the next week cycle based on two dates
body: <p>I have an event system set up and it works in cycles. For example, users can create events in 2, 4, 6 and 8 weekly cycles. Eventually this will be user definable.</p>
  <p>I need to be able to calculate the current cycle for any future events that are not actually generated.</p>
  <p>So e.g. if a user has selected a 6-week event cycle, and 9th August is cycle week 5, 16th August will be week 6, the 23rd will then be week 1, and so forth, continuously looping back through the cycle for each week. If we get all the way down to 15th November for example, that will be cycle week 1 again.</p>
  <pre><code>&quot;2021-08-09&quot;
  5
  &quot;2021-08-16&quot;
  6
  &quot;2021-08-23&quot;
  1
  &quot;2021-08-30&quot;
  2
  &quot;2021-09-06&quot;
  3
  </code></pre>
  <p>What would be the best way for me to calculate the current week cycle if I know the start of the cycle and the current date? Hopefully that makes sense?</p>
  <p>I have the following at the moment, but a loop is going to be quite expensive I'd have thought if I'm calculating this in 5-10 years time.</p>
  <pre class="lang-php prettyprint-override"><code>$start = Carbon\Carbon::parse('2021-08-09');
  $end = Carbon\Carbon::parse('2021-11-15');
  
  $cycle_weeks = 6;
  $current_cycle = 5;
  
  dump(&quot;Week - {$start-&gt;toDateString()} - Cycle {$current_cycle}&quot;);
  
  // Get current cycle week
  while ($start-&gt;lt($end)) {
      
      $start = $start-&gt;addWeeks(1);
      $current_cycle = $current_cycle + 1;
      
      if ($current_cycle &gt; $cycle_weeks) {
          $current_cycle = 1;
      }
      
      dump(&quot;Week - {$start-&gt;toDateString()} - Cycle {$current_cycle}&quot;);
  }
  </code></pre>
  <p>This gives me the date but at the expense of cycling through every week until the end date. I can't think of a mathematical way - if any - to perhaps work out the cycle based on the difference in weeks?</p>
  <pre><code>&quot;Week - 2021-08-09 - Cycle 5&quot;
  &quot;Week - 2021-08-16 - Cycle 6&quot;
  &quot;Week - 2021-08-23 - Cycle 1&quot;
  &quot;Week - 2021-08-30 - Cycle 2&quot;
  &quot;Week - 2021-09-06 - Cycle 3&quot;
  &quot;Week - 2021-09-13 - Cycle 4&quot;
  &quot;Week - 2021-09-20 - Cycle 5&quot;
  &quot;Week - 2021-09-27 - Cycle 6&quot;
  &quot;Week - 2021-10-04 - Cycle 1&quot;
  &quot;Week - 2021-10-11 - Cycle 2&quot;
  &quot;Week - 2021-10-18 - Cycle 3&quot;
  &quot;Week - 2021-10-25 - Cycle 4&quot;
  &quot;Week - 2021-11-01 - Cycle 5&quot;
  &quot;Week - 2021-11-08 - Cycle 6&quot;
  &quot;Week - 2021-11-15 - Cycle 1&quot;
  </code></pre>