I writing a script where it will populate a series of dates based on the criteria set between a start and finish time.
I basically using the relative formats in php dates function (http://php.net/manual/en/datetime.formats.relative.php).
My script first get the intervals based on user's selection:
//first get the interval
$event_repeatmonth = $_POST['event_repeatmonth'];
if ($event_repeatmonth == 1){ $interval = 'first';}
if ($event_repeatmonth == 2){ $interval = 'second';}
if ($event_repeatmonth == 3){ $interval = 'third';}
if ($event_repeatmonth == 4){ $interval = 'fourth';}
if ($event_repeatmonth == 5){ $interval = 'fifth';}
if ($event_repeatmonth == 6){ $interval = 'last';}
$onthe_monday = $_POST['onthe_monday'];
$onthe_tuesday = $_POST['onthe_tuesday'];
$onthe_wednesday = $_POST['onthe_wednesday'];
$onthe_thursday = $_POST['onthe_thursday'];
$onthe_friday = $_POST['onthe_friday'];
$onthe_saturday = $_POST['onthe_saturday'];
$onthe_sunday = $_POST['onthe_sunday'];
if ($onthe_monday == 1){ $interval_period[] = $interval." Monday";}
if ($onthe_tuesday == 1){ $interval_period[] = $interval." Tuesday";}
if ($onthe_wednesday == 1){ $interval_period[] = $interval." Wednesday";}
if ($onthe_thursday == 1){ $interval_period[] = $interval. " Thursday";}
if ($onthe_friday == 1){ $interval_period[] = $interval. " Friday";}
if ($onthe_saturday == 1){ $interval_period[] = $interval. " Saturday";}
if ($onthe_sunday == 1){ $interval_period[] = $interval. " Sunday";}
Then it will loop through the dates within the start and finish date. It will grab the first date of the month as the reference date and find the corresponding 1st/2nd/3rd Monday/Tuesday... of the month:
//loop through to insert the array
while ($tmp <= $event_udate){
$tmpmonth = date("Y-m", strtotime($tmp))."-01";
//get proper date based on the intervals we are looking for
foreach ($interval_period AS $period){
$tmp = date("Y-m-d", strtotime($period, strtotime($tmpmonth)));
echo $period."--".$tmp."--".$tmpmonth."<br/>";
//sometimes the first interval will be before the start date, skip that
if ($tmp >= $event_sdate){
$event_datearray[] = $tmp;
}
}
//reset if reach end of the month
if ($repeat_every > 1){
//go to next month
$tmp = date('Y-m-d', strtotime('+'. $repeat_every.' month', strtotime($tmp)));
}else{
//go to next month
$tmp = date('Y-m-d', strtotime('+1 month', strtotime($tmp)));
}
}
For example, here I asked the program to get the second Wednesday and the second Sunday of the month for every 3 months between the time frame of 4/1/2015 - 8/30/2015.
The result should have returned: 4/8, 4/12, 7/8, and 7/12. However, it appears that since 4/1 and 7/1 are on Wednesday, it skipped the first week and grabbed the 3rd Wednesday instead.
My echo result looks like below, as you can see, it grabbed 4/15 as the 2nd Wednesday of 2015-04-01.... and grabbed 7/15 as the 2nd Wednesday of 2015-07-01...
second Wednesday--2015-04-15--2015-04-01
second Sunday--2015-04-12--2015-04-01
second Wednesday--2015-07-15--2015-07-01
second Sunday--2015-07-12--2015-07-01
array(4) { [0]=> string(10) "2015-04-15" [1]=> string(10) "2015-04-12" [2]=> string(10) "2015-07-15" [3]=> string(10) "2015-07-12" }
I am pretty lost here. This seems to be issue only when the first of the month matches one of the criteria. If I change it to 2nd Thursday and 2nd Sunday, then it will return properly:
second Thursday--2015-04-09--2015-04-01
second Sunday--2015-04-12--2015-04-01
second Thursday--2015-07-09--2015-07-01
second Sunday--2015-07-12--2015-07-01
array(4) { [0]=> string(10) "2015-04-09" [1]=> string(10) "2015-04-12" [2]=> string(10) "2015-07-09" [3]=> string(10) "2015-07-12" }
Does anyone have the same issue before???