'How to repeat event for every 2 weeks on multi selection days up to 3 Occurrence's

I'm working on a google calendar kind of repeat feature using angularjs/php. enter image description here

Trying repeat the event for every 2 weeks on multi selection of days and that event will be repeated up to 3 occurrence's (times) from start date. PFA my code.

$scope.daysarr = [{dayname: "SUN",Name: 'Sunday', Selected: true},
{dayname: "MON",Name: 'Monday', Selected: false},
{dayname: "TUE",Name: 'Tuesday', Selected: true},
{dayname: "WED",Name: 'Wednesday', Selected: false},
{dayname: "THU",Name: 'Thursday', Selected: true},
{dayname: "FRI",Name: 'Friday', Selected: false},
{dayname: "SAT",Name: 'Saturday', Selected: false}];

for ($i=1; $i<=$after_occurance ; $i++) { // 3times
if($i==1)
{
    $datesArr[] = $date->format('Y-m-d');
}
else
{
    echo $dayOfWeek = $date->format('l');
    if ($dayOfWeek == 'Sunday' && $daysarr[0]->selected==1) { // i want to loop sun,tue,thu
        $date->modify("+{$repeatnum} weeks");   // 2 weeks
            $datesArr[] = $date->format('Y-m-d');
    }
    if ($dayOfWeek == 'Tuesday' && $daysarr[2]->selected==1) { // i want to loop sun,tue,thu
        $date->modify("+{$repeatnum} weeks");   // 2 weeks
            $datesArr[] = $date->format('Y-m-d');
    }
    if ($dayOfWeek == 'Thursday' && $daysarr[4]->selected==1) { // i want to loop sun,tue,thu
        $date->modify("+{$repeatnum} weeks");   // 2 weeks
            $datesArr[] = $date->format('Y-m-d');
    }
            
}}

Getting output only selected start date in repeat instead of selected days

expected output:

sun: 09-jan-2022, 23-jan-2022, 06-feb-2022

tue: 04-jan-2022, 18-jan-2022, 01-feb-2022

thu: 06-jan-2022, 20-jan-2022, 03-feb-2022



Solution 1:[1]

I would do it using seconds based on the provided startDate converted to EPOCH:

<?php
   // convert startDate input to epoch time
   $startDate = strtotime($_POST['startDate']);

   // how many seconds does 2 weeks contain?
   $interval = 2 * 7 * 24 * 60 * 60;

   // calculate and display the startDate, followed by its next ocurrences
   for ($i=0; $i<$after_occurance; $i++) { // 3times
      if ($i == 0) {
         // output the first date with the Day name
         echo date("D, d-m-Y", ($startDate + ($i * $interval)));
      } else {
         // all other dates won't display the day name
         echo date("d-m-Y", ($startDate + ($i * $interval)));
      }
      // if not last, add a comma and a space to the output
      if ($i < $after_occurance-1) { 
          echo ', ';
      } else {
          echo '<br/>';
      }
   }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mindthefrequency