0

I am new to PHP. I am displaying the monthly events which is starting event from 03/31/2021 on every month, so here is a critical situation come because not every month exists 31 date. So there is some php function available to check this date is exists in next coming months. If exists then display the event on that date otherwise display the last date of the month.

$monthly_counter = 0;
$monthly_counter_offset = 0;
$begin = new DateTime($val->start);  // $val->start = '03/31/2021'
$end = new DateTime($val->repeat_end_date); // $val->repeat_end_date= '03/31/2022'
$interval = DateInterval::createFromDateString('1 month');
$period = new DatePeriod($begin, $interval, $end);
    foreach ($period as $dt)
        {
            $loop_date = $dt->format('m/d/Y');
            $loop_date_formatted = $dt->format('Y-m-d');

            $due_dates[] = $from_date_times;
            $from_new_date_times = date('Y-m-d H:i', strtotime('+1 month', strtotime($from_date_times)));
            $from_date_times = $from_new_date_times;
            $deletedevents2=$val->deletedevents;
            $arrayofdeleted=explode(",",$deletedevents2);
            $originalDate = $from_date_times;
            $newDateforcompare = date("m/d/Y", strtotime($originalDate));
              if (in_array($loop_date_formatted, $arrayofdeleted))
                {
                    continue;
                }

                $number_of_days = $val->number_of_days;
                $end_new_date_times = date('Y-m-d H:i', strtotime('+'.$number_of_days.' day', strtotime($loop_date)));
                if(date('Y-m-d ', strtotime($val->start))<date('Y-m-d', strtotime($val->ends))) 
                    {
                    $end_new_date_times = date('Y-m-d H:i', strtotime('+1 day', strtotime($end_new_date_times)));
                    }

                        $skipcounter = $val->interval_value;
                        if (!empty($skipcounter) || $skipcounter != 1) {
                            $monthly_counter++;
                            $monthly_counter_offset++;
                            if ($monthly_counter == $skipcounter || $monthly_counter_offset == 1) {
                                $monthly_counter = 0;
                            } else {
                                continue;
                            }
                        } else {
                        }


                        $rows[] = array(
                            'id' => $val->id,
                            'title' => $val->title,
                            'description' => $val->calendar_comments,
                            'start' => $loop_date,
                            'end' => $end_new_date_times,
                            'borderColor'=>$val->color,
                            'backgroundColor'=>$val->color_bg,
                            'className'=>'timegridclass',
                            'allDay' => $allday,

                        );
                    }

1 Answers1

0

Since you're using fullCalendar, you can simply specify an event which uses RRule to specify the recurrence, rather than using complex PHP code to try and generate it.

e.g.

  events: [
    {
      title: "Sales Meeting",
      rrule: "FREQ=MONTHLY;BYMONTHDAY=28,29,30,31;BYSETPOS=-1"
    }
  ]

Working demo: https://codepen.io/ADyson82/pen/bGByBaV

This will generate an event which repeats on the last day of every month, regardless whether the month is 28, 29, 30 or 31 days long.

Obviously you can use PHP to generate this event object, and enhance it with a custom title, start/end dates etc as per your database contents. But I have shown you the basic approach.

Credit to this answer for the specific RRule string.

Documentation: https://fullcalendar.io/docs/rrule-plugin and https://github.com/jakubroztocil/rrule

ADyson
  • 57,178
  • 14
  • 51
  • 63
  • yes really this is a very easy and simple way to handle but some code is adding by the client so i just follow the code and requirements – Alex Robbio Mar 17 '21 at 12:18