Frage

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.

War es hilfreich?

Lösung

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/>';
    }
}

Andere Tipps

I don't know if it is what you are looking for, but it's much better then go through all dates. When you know the the first Mon/wed/Fri

For Example:

<?php
$interval = 3;
$monday = date('Y-m-d',strtotime('next Monday'));
$wednesday = date('Y-m-d',strtotime('next Wednesday'));
$friday = date('Y-m-d',strtotime('next Friday'));

for($i=0; $i<$interval;++$i){   
    echo $monday."\n";
    echo $wednesday."\n";
    echo $friday."\n";
    $monday =  date('Y-m-d', strtotime($monday . ' +1 Week'));
    $wednesday =  date('Y-m-d', strtotime($wednesday . ' +1 Week'));
    $friday =  date('Y-m-d', strtotime($friday . ' +1 Week'));
}                       

?>

You can do this in a loop and don't have to go through all dates.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top