4

I am trying to handle groups which meet either once a week, twice a month or once a month - on any day of the week. I would like to calculate the number of occurrences any given configuration would generate for a given time frame.

For example, say a group meets every Friday, from Jan 1 to June 30, how many times would they meet? Or, if a group meets on Tuesday every other week, from Jan 1 to June 30, how many times would they meet? Etc.

Is this possible in php? I'm not seeing a solution in the DateInterval manual.

Thank you for any help.

4

2 に答える 2

2

まず、2つの日付の間にN日がある場合、各曜日の少なくともフロア(N / 7)があることを知っています。しかし、それはそれより1つ多いかもしれません。

たとえば、今年(2012年)の1月1日から6月30日までは182日です(両方のエンドポイントが含まれていると仮定)。これは正確に26週間なので、曜日は正確に26週間あります。ただし、来年の1月1日から6月30日までは、わずか181日、つまり25週間プラス6日です。たまたま26金曜日ですが、月曜日は25日だけです。

上記の「重複の可能性がある」リンクからの@philmccullの回答をお勧めします。

于 2012-05-22T00:41:47.910 に答える
1

だから、私はそれの要点は

1) 期間内にいくつの「期間」があるかを計算します (したがって、期間が 35 日間で、会議が隔週である場合、2 つの完全なセット (28 日間) があります)

2) 全サイクル外の残りの日数を調べる (基本的に係数)

合計は次のようになります: (ピリオドの数)*(ピリオドごとのミーティング日数)+(残りのミーティング日数)

これが最も効率的なコードであると言っているわけではありません (入力検証もありません) が、これを達成する方法は次のとおりです。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php
if(isset($_REQUEST["start"])){
    $cycle=$_REQUEST["recur"]*604800;//number of weeks to seconds
    $start=strtotime($_REQUEST["start"]);
    $end=strtotime($_REQUEST["end"])+(24*60*60);//end has been extended to be inclusive
    $duration=$end-$start;
    $periods=floor($duration/$cycle);
    $remainder_start=$start+($periods*$cycle);
    $number_of_days=($end-$remainder_start);
    while($number_of_days>604800){//this piece reduces the set to test to just the final week of the remainder (if a meeting is every three weeks, you don't need to test the first 14 days of the remainder)
        $number_of_days=$number_of_days-604800;
        $remainder_start=$remainder_start+604800;
        }
    $number_of_days=$number_of_days/(24*60*60);
    $d=getdate($remainder_start+1);
    $rem_start_day=$d["wday"];//0=sunday

    $days=0;
    if($_REQUEST["sunday"]=='true'){$sunday=true;$days++;}
    if($_REQUEST["monday"]=='true'){$monday=true;$days++;}
    if($_REQUEST["tuesday"]=='true'){$tuesday=true;$days++;}
    if($_REQUEST["wednesday"]=='true'){$wednesday=true;$days++;}
    if($_REQUEST["thursday"]=='true'){$thursday=true;$days++;}
    if($_REQUEST["friday"]=='true'){$friday=true;$days++;}
    if($_REQUEST["saturday"]=='true'){$saturday=true;$days++;}

    $total=$days*$periods;
    $n=$number_of_days;
    for($i=0; $i<$n; $i++){
        switch($rem_start_day){
            case 0:if($sunday){$total++;}break;
            case 1:if($monday){$total++;}break;
            case 2:if($tuesday){$total++;}break;
            case 3:if($wednesday){$total++;}break;
            case 4:if($thursday){$total++;}break;
            case 5:if($friday){$total++;}break;
            case 6:if($saturday){$total++;}break;
        }
        $rem_start_day++;
        $rem_start_day=$rem_start_day%7;
    }
    echo "NUMBER OF INSTANCES:".$total."<br/>";
}
else{
?>
<form action="" method="post">
Start:<input type="text" name="start" placeholder="YYYY-MM-DD"/><br/>
End:<input type="text" name="end" placeholder="YYYY-MM-DD"/><br/>
<table>
<tr><td>Sn</td><td>M</td><td>T</td><td>W</td><td>R</td><td>F</td><td>St</td></tr>
<tr><td><input type="checkbox" name="sunday" value="true"/></td><td><input type="checkbox" name="monday"  value="true"/></td><td><input type="checkbox" name="tuesday"  value="true"/></td><td><input type="checkbox" name="wednesday"  value="true"/></td><td><input type="checkbox" name="thursday"  value="true"/></td><td><input type="checkbox" name="friday"  value="true"/></td><td><input type="checkbox" name="saturday"  value="true"/></td></tr></table>
Occurs Every <input type="text" name="recur" style="width:30px"/> Week(s)<br/>
<input type="submit" id="button" value="Go" />
<?php
}
?>
</body>
</html>
于 2012-05-22T02:54:47.487 に答える