简体   繁体   English

发生算法

[英]Algorithm for occurrence

Can someone help me figuring out how to get if a current week is inside an occurrence . 有人能帮我弄清楚如果当前一周在事件发生时如何得到。

I have the following vars : start_date , end_date , current_date week_occurrence . 我有以下变量:start_date,end_date,current_date week_occurrence。

and i have a function that return the # of occurrence 我有一个函数返回出现的次数

// will return the number of weeks between start - end
function get_weeks_count($start , $end) {
       return floor(abs(strtotime($start) - strtotime($end)) / 604800);    
    }

now i have to know if a current date is a valid date . 现在我必须知道当前日期是否是有效日期。

I have an entry with occurrence = every N weeks . 我有一个条目,每N周出现一次。 How to know that N is valid . 如何知道N是有效的。

Less abstract : If we are in December and the occurrence is every 3 weeks , start_date is 1st and end_date is 30 December) 不太抽象:如果我们在12月,并且发生的间隔是每3周一次,则start_date为1st,end_date为12月30日)

It will return : 它将返回:

 TRUE  for 1st week

 FALSE for the second week

 FALSE for the third week

 TRUE  for the last week

Here's how I would approach the problem - this applies for an occurrence every $n weeks. 这是解决问题的方式-这适用于每$ n周发生一次。

$n = $week_occurrence;
$occurrence = false;

// To begin, get the number of weeks between the start and current dates.
$weeks = get_weeks_count($start_date , $current_date); // Using the function you already have

// Now check if $weeks == 0
if ($weeks == 0) {
    $occurrence = true;

// If not, check if $weeks is divisible by $n without any remainder
} else if ($weeks % $n == 0) {
    $occurrence = true;
}

If $occurrence is still false then the current week does not fall within the the correct occurrence, if it's true then the week does fall within the scope. 如果$occurrence仍然为false,则当前星期不在正确的范围内;如果为true,则该星期确实在范围内。

Effectively all we're doing here is checking that the current number of weeks since the start date is either equal to zero (we're still in the first week) or is divisible by the ocurrence without a remainder. 实际上,我们在这里所做的所有事情都是检查自开始日期以来的当前周数是否等于零(我们仍在第一周)或是否可被发生次数整除。

I hope this helps. 我希望这有帮助。

PS I've only answered the specific question that you asked. 附言:我只回答了您提出的具体问题。 However, if you would like to know more about how this premiss could be used for scheduling etc., then feel free to ask and I'll expand on my answer accordingly 但是,如果您想了解更多有关如何将此前提条件用于计划等的信息,请随时提出,我将相应地扩展解答

A combination of DateTime and DateInterval should help you achieve this easily. DateTimeDateInterval的组合应该可以帮助您轻松实现这一目标。

function get_occurences(DateTime $start, DateTime $end, DateInterval $period) {
    $weeks = array();
    $cursor = clone $start;
    $rate = DateInterval::createFromDateString('1 week');
    do {
        /* We can check to see if it's within the occurrence period */
        if ($cursor == $start) {
            $isOccurrence = true;
            $start->add($period); // Move the start period up
        } else {
            $isOccurrence = false;
        }
        $weeks[$cursor->format('Y-m-d')] = $isOccurrence;
    } while($cursor->add($rate) < $end);
    return $weeks;
}

$period = DateInterval::createFromDateString('3 week');
$start = new DateTime('2012-12-01');
$end = new DateTime('2012-12-30');
/* From this array you can get both the number of occurrences as well as their respective dates*/
var_dump(get_occurences($start, $end, $period));

/** Output:

    array(5) {
      ["2012-12-01"]=>
      bool(true)
      ["2012-12-08"]=>
      bool(false)
      ["2012-12-15"]=>
      bool(false)
      ["2012-12-22"]=>
      bool(true)
      ["2012-12-29"]=>
      bool(false)
    }

*/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM