Search code examples
phpdatetimedateintervalrecurring-events

PHP DateInterval select days of week every x weeks


I've written some code for entering recurring events. The trickiest part is a user can create an event and have it repeat on selected days of the week ever x weeks.

For example, the user can select Mon,Wed,Fri and have it repeat every 3 weeks.

Easy to do if every week- I can do something like this:

$start = new DateTime( '2013-10-01' );
$end = new DateTime( '2014-01-01 23:59:59' );
$interval = new DateInterval('P1D');

$period = new DatePeriod($start, $interval, $end);
foreach ($period as $date) {
    $dayOfWeek = $date->format('l');
    if ($dayOfWeek == 'Monday' || $dayOfWeek == 'Wednesday' || $dayOfWeek == 'Wednesday') {
        // do something
    }
}

For the above code, how would I modify it to include Mon/Wed/Fri every 3 weeks? Is there a better way to achieve this without looping through every day?

I'm thinking I can use $date->format('w'); which would give me the numeric week of the year. I could then have a counter and reset it.


Solution

  • You can check if the week changes inside the foreach as a possible approach.

    <?php
    
    $start = new DateTime( '2013-10-01' );
    $end = new DateTime( '2014-01-01 23:59:59' );
    $interval = new DateInterval('P1D');
    
    $period = new DatePeriod($start, $interval, $end);
    
    // only trigger every three weeks...
    $weekInterval = 3;
    
    // initialize fake week
    $fakeWeek = 0;
    $currentWeek = $start->format('W');
    
    foreach ($period as $date) {
    
        if ($date->format('W') !== $currentWeek) {
            $currentWeek = $date->format('W');
            $fakeWeek++;
            print ' WEEK ' . $currentWeek . '<br/>';
        }
    
        if ($fakeWeek % $weekInterval !== 0) {
            continue;
        }
    
        $dayOfWeek = $date->format('l');
        if ($dayOfWeek == 'Monday' || $dayOfWeek == 'Wednesday' || $dayOfWeek == 'Friday') {
            print $date->format('Y-m-d H:i:s') . '   ' . $dayOfWeek . '<br/>';
        }
    }