Php 发生算法
有没有人能帮我弄清楚,如果一周内发生了什么事,该怎么办 我有以下变量:开始日期、结束日期、当前日期周事件 我有一个函数,返回Php 发生算法,php,algorithm,Php,Algorithm,有没有人能帮我弄清楚,如果一周内发生了什么事,该怎么办 我有以下变量:开始日期、结束日期、当前日期周事件 我有一个函数,返回 // will return the number of weeks between start - end function get_weeks_count($start , $end) { return floor(abs(strtotime($start) - strtotime($end)) / 604800); } 现在我必须知道
// will return the number of weeks between start - end
function get_weeks_count($start , $end) {
return floor(abs(strtotime($start) - strtotime($end)) / 604800);
}
现在我必须知道当前日期是否有效
我有一个条目,其发生率=每N周一次。如何知道N是有效的
不那么抽象:如果我们是在12月,并且每3周发生一次,开始日期是1日,结束日期是12月30日)
它将返回:
TRUE for 1st week
FALSE for the second week
FALSE for the third week
TRUE for the last week
下面是我如何解决这个问题的方法-这适用于每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;
}
如果$occurrence
仍然为false,则当前周不在正确的事件范围内,如果为true,则该周不在范围内
实际上,我们在这里所做的只是检查自开始日期起的当前周数是否等于零(我们仍然在第一周),或者是否可以被当前数整除,而不带余数
我希望这有帮助
另外,我只回答了你提出的具体问题。但是,如果您想了解更多有关如何将premiss用于日程安排等方面的信息,请随时提问,我将相应地扩展我的答案,并结合和将帮助您轻松实现这一点
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)
}
*/
函数get_occurrences(DateTime$start,DateTime$end,DateInterval$period){
$weeks=array();
$cursor=clone$start;
$rate=DateInterval::createFromDateString('1周');
做{
/*我们可以检查它是否在发生期内*/
如果($cursor==$start){
$isOccurrence=true;
$start->add($period);//向上移动开始时段
}否则{
$isOccurrence=false;
}
$weeks[$cursor->format('Y-m-d')]=$isocurrence;
}而($cursor->add($rate)<$end);
返回$weeks;
}
$period=DateInterval::createFromDateString('3周');
$start=新日期时间('2012-12-01');
$end=新日期时间('2012-12-30');
/*从该数组中,您可以获得事件数及其各自的日期*/
变量转储(获取事件($start、$end、$period));
/**输出:
阵列(5){
["2012-12-01"]=>
布尔(真)
["2012-12-08"]=>
布尔(假)
["2012-12-15"]=>
布尔(假)
["2012-12-22"]=>
布尔(真)
["2012-12-29"]=>
布尔(假)
}
*/
如果您告诉我们您使用的语言,可能有人会找到一个库函数来实现this@BlackBear在我看来像PHP:TaggedGiven N周周期的第Mth次出现,有开始和结束(假设结束包括在内),那么您的日期是有效的,如果开始+7MN只是为了澄清,您是否正在尝试编写允许您处理调度的算法?i、 e.每3天,或每个月的第一个星期一?是的,Gavin,但这只是几周,就像每2周、每3周、每7周……我认为这不会起作用,因为在大多数情况下,当你测试($weeks%$n==0)时,它将返回零,假设你的开始日期为2012-11-01,结束日期为2013-12-08,这里的$weeks等于~57,所以。。。任何57%n(其中n@Commandos不,请看代码。Weeks是自开始日期(即开始日期到当前日期)起的周数。模数返回余数,而不是一个数字转换到另一个数字的次数。因此57%35将返回22。如果您有任何其他问题,请提问。