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 .
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 . How to know that N is valid .
Less abstract : If we are in December and the occurrence is every 3 weeks , start_date is 1st and end_date is 30 December)
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 = $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.
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.
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)
}
*/
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.