Php 检查在同一时间发生(但可以在不同的日子重复)的重复事件是否跨越自身
我有一个事件,有开始日期,“2013-12-06 21:00:00”,持续时间和重复天数。 问题是,用户可以指定持续时间>24小时,并选择事件将跨越自身的重复天数 比如说Php 检查在同一时间发生(但可以在不同的日子重复)的重复事件是否跨越自身,php,validation,date,recurrence,Php,Validation,Date,Recurrence,我有一个事件,有开始日期,“2013-12-06 21:00:00”,持续时间和重复天数。 问题是,用户可以指定持续时间>24小时,并选择事件将跨越自身的重复天数 比如说 事件持续时间为25小时,用户选择每天重复,因此它将跨越自身 事件持续时间为49小时,用户选择星期一和星期三,所以事件将再次交叉 P>有必要考虑验证应在星期一星期日之后进行事件交叉检查。< / P> 你能回顾一下我的解决方案吗?也许可以通过某种方式进行优化 // Input params $start = '2013-12-0
// Input params
$start = '2013-12-06 12:42:00';
$duration = 60 * 60 * 24 + 1; // specify duration
$days = '1000001'; // monday is at 0 position, sunday is at 6
// Loop internal vars
$dayLength = 60 * 60 * 24;
$loopStart = strtotime($start);
// had to take 8 days to check all week
$loopEnd = $loopStart + ($dayLength * 8);
$previousDateEnd = null;
for ($loop = $loopStart; $loop < $loopEnd; $loop += $dayLength) {
$weekday = date('N', $loop) - 1;
if ($days{$weekday}) {
if (!is_null($previousDateEnd) && $loop < $previousDateEnd) {
die('Event cross itself!');
}
$previousDateEnd = $loop + $duration;
}
}
优化版本,修复了问题中描述的错误
// Input params
$start = '2013-12-06 12:42:00';
$duration = 60 * 60 * 24 * 3;
$repeatDays = '0001001'; // monday is at 0 position, sunday is at 6
$requiredDaysOffset = ceil($duration / (60 * 60 * 24)) - 1;
for($i = 0; $i < 7; $i++) {
if ($repeatDays[$i]) {
echo 'index: ' . $i . ' ';
// Check that next $requiredDaysOffset days are disabled
$pos = $i;
echo 'check next ' . $requiredDaysOffset . ' days: ';
for($j = 0; $j < $requiredDaysOffset; $j++) {
$pos = $pos < 6 ? ($pos + 1) : 0;
echo "\npos: " . $pos . " ? " . $repeatDays{$pos};
if ($repeatDays{$pos}) {
echo " cross!";
}
}
echo "\n";
}
}
//输入参数
$start='2013-12-06 12:42:00';
$duration=60*60*24*3;
$repeatDays='0001001';//周一是0档,周日是6档
$requiredDaysOffset=ceil($duration/(60*60*24))-1;
对于($i=0;$i<7;$i++){
如果($repeatDays[$i]){
echo“索引:”.$i.”;
//检查是否禁用了接下来的$RequiredDaysofset天
$pos=$i;
回显“下一步检查”。$requiredDaysofset.“天:”;
对于($j=0;$j<$requiredDaysofset;$j++){
$pos=$pos<6?($pos+1):0;
echo“\npos:“.$pos.”?“$repeatDays{$pos};
如果($repeatDays{$pos}){
回音“十字架!”;
}
}
回音“\n”;
}
}
// Input params
$start = '2013-12-06 12:42:00';
$duration = 60 * 60 * 24 * 3;
$repeatDays = '0001001'; // monday is at 0 position, sunday is at 6
$requiredDaysOffset = ceil($duration / (60 * 60 * 24)) - 1;
for($i = 0; $i < 7; $i++) {
if ($repeatDays[$i]) {
echo 'index: ' . $i . ' ';
// Check that next $requiredDaysOffset days are disabled
$pos = $i;
echo 'check next ' . $requiredDaysOffset . ' days: ';
for($j = 0; $j < $requiredDaysOffset; $j++) {
$pos = $pos < 6 ? ($pos + 1) : 0;
echo "\npos: " . $pos . " ? " . $repeatDays{$pos};
if ($repeatDays{$pos}) {
echo " cross!";
}
}
echo "\n";
}
}