【发布时间】:2015-03-06 21:55:40
【问题描述】:
我编写了一个脚本,它将根据在开始时间和结束时间之间设置的条件填充一系列日期。
我基本上使用 php 日期函数 (http://php.net/manual/en/datetime.formats.relative.php) 中的相对格式。
我的脚本首先根据用户的选择获取间隔:
//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";}
然后它将遍历开始日期和结束日期内的日期。它将抓取当月的第一个日期作为参考日期,并找到该月对应的第 1/2/3 个星期一/星期二...:
//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)));
}
}
例如,在这里我要求程序在 2015 年 4 月 1 日到 2015 年 8 月 30 日之间每 3 个月获取每月的第二个星期三和第二个星期日。
结果应该返回:4/8、4/12、7/8 和 7/12。但是,由于 4/1 和 7/1 是在星期三,它似乎跳过了第一周,而是抓住了第三个星期三。
我的回显结果如下所示,如您所见,它在 2015 年 4 月 1 日的第二个星期三抓取了 4/15.... 并在 2015 年 7 月 1 日的第二个星期三抓取了 7/15。 ..
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" }
我在这里迷路了。这似乎只有在当月的第一天符合其中一个标准时才会出现。如果我将其更改为第 2 个星期四和第 2 个星期日,那么它将正确返回:
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" }
以前有人遇到过同样的问题吗???
【问题讨论】: