如何查找一周中特定日期在给定时间段内出现的次数


How to find the number of times a specific day of the week occurs during a given time period

我正在尝试处理每周一次,每月两次或每月一次的小组 - 在一周中的任何一天。 我想计算任何给定配置在给定时间范围内生成的出现次数。

例如,假设一个小组从 1 月 1 日到 6 月 30 日的每个星期五开会,他们会开多少次会议? 或者,如果一个小组每隔一周在星期二开会,从 1 月 1 日到 6 月 30 日,他们会开会多少次? 等。

这在 php 中可能吗? 我在日期间隔手册中没有看到解决方案。

感谢您的任何帮助。

嗯,首先,你知道如果两个日期之间有N天,那么一周中的每一天至少都有地板(N/7(。 但它可能不止一个。

例如,从今年(2012 年(的 1 月 1 日到 6 月 30 日是 182 天(假设包括两个端点(。 这正好是 26 周,所以一周中的每一天正好有 26 周。然而,从1月1日到明年6月30日,只有181天,即25周加6天。它也恰好是26个星期五,但只有25个星期一。

我从上面的"可能重复"链接中推荐@philmccull的答案。

所以,我认为它的要点是

1(计算出跨度中有多少个"周期"(因此,如果跨度是35天,而会议是每隔一周,那么您有两个完整的集合(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>