如何知道两个日期的一周中的最短时间段,以不同的频率增加,重叠


how to know the minimum period of week for two date, increasing by different frequence, to overlap

开始我很抱歉我的英语不好,我希望你能理解我的问题并找到解决方案......

我的问题是我有两个时期

the first period:  dateStart1-dateEnd1
the secondperiod:  dateStart2-dateEnd2

对于第一对夫妇,频率 = 2 :

dte=dateStar1;dateEnd1>dte;dte+2week

对于第二个,频率 = 3 :

dte=dateStar2;dateEnd2>dte;dte+3week

示例:

first period  2016-04-04 -> 2016-05-09 
frequence 2 weeks 2016-04-04 , 2016-04-18 , 2016-05-02
the second : 2016-04-11 -> 2016-05-09 
frequence 3 weeks 2016-04-11, 2016-05-02 
the two periods overlaps in 2016-05-02

我的问题是如何知道两个时期或日期重叠的最小周数?谢谢

可能有更好的算法,但我唯一能想到的是检查一段时间内所有可用的周数。

$result = [];
$current = clone $dateStart1;
while ($current < $dateEnd1) {
    // Calculate the difference betweenthe current date and dateStart2
    $diff = $current->diff($dateStart2);
    // Check if the difference is a multiple of the frequency, i.e. if the number of days is congruent to 7 times the frequency
    if ($diff->days % ($frequency2 * 7) == 0) {
        // If the anwser is yes, adds the current date to the list of overlapping dates.
        $result[] = clone $current;
    }
    // Get the next date possible for period 1 by adding $frequency1 weeks to the current date, and then try again till $dateEnd1 is reached.
    $current = $current->add(new 'DateInterval('PT'.$frequency1.'W'););
}
return $result;

我还没有完全测试它,但这至少可以帮助您上轨。