0

I'm making a function so that I can make recurring events.

The first thing I need to do is duplicate the event date at set intervals. For example (August 2, 2019) with 1 week interval will produce; August 9, 2019 August 16, 2019 August 23, 2019 and so on. I want to be able to set the number of intervals. Like 4 weeks, 5... 100 weeks into the future.

Here is my attempt: 1. I don't know how to take the $newdate and then input that into the $event_date slot with each loop. 2. I don't know how to output the set of new dates in an array.

<?php
function recurring_event($event_date, $num_days=''){
for($i=0 ; $i < 8 ; $i++ ){
$date = date_create($event_date);
$newdate = date_add($date, date_interval_create_from_date_string($num_days));



echo date_format($newdate, 'D, F jS, Y');

}
}


recurring_event($event_date, '7days');
?>

I'm too novice to actually conceptualize how I can tackle this problem. I've tried searching this site, the PHP manual.. Tried for() loops, continue logic, etc.

Any help with this problem would be greatly appreciated.

2 Answers2

0

You should try

$recurringDate = strtotime($event_date, '+ 7 days');

Strtotime is a wonder, have a look : https://www.php.net/manual/en/function.strtotime.php

Will
  • 899
  • 8
  • 15
0

You can try like this:

function recurringEvent($eventDate, $numWeek = 1){
    $result= [];
    for($i = 1; $i <= $numWeek; $i++){
        $nextEvent = date('D, F jS, Y', strtotime($eventDate." +7 days"));
        $eventDate = $nextEvent;
        $result[] = $nextEvent;
    }
    return $result;

}


$date = date("D, F jS, Y"); // current date or fixed a date: $date = date("D, F jS, Y", strtotime('Sat, August 3th, 2019'));
echo '<pre>';
print_r(recurringEvent($date,6));
Trung
  • 135
  • 1
  • 13