【发布时间】:2013-05-22 15:34:15
【问题描述】:
我正在尝试创建一个递归函数,以根据某些规则生成一组有效日期。
到目前为止我已经有了这个功能
function progDateRange($date, $end_date, $wkDays, $excluded, $dates = array())
{
$valid_date = false;
$max_date = new DateTime(date('Y-m-d'));
$max_date->add(new DateInterval('P2Y'));
$max_date = $max_date->format('Y-m-d');
// is this date before the end date or the max date
if(strtotime($date) <= strtotime($end_date) && strtotime($date) <= strtotime($max_date))
{
if(!in_array($date, $excluded))
{
foreach($wkDays as $day => $val)
{
// is this date a valid weekday start
if(date("l", strtotime($date)) == $day) {
// successful date
$valid_date = true;
}
}
if($valid_date) {
array_push($dates, $date);
}
}
$next_day = new DateTime($date);
$next_day->add(new DateInterval('P1D'));
progDateRange($next_day->format('Y-m-d'), $end_date, $wkDays, $excluded, $dates);
} else {
return $dates;
}
}
我在单独的页面上使用它
$datesArray = progDateRange($date_start, $date_end, $wkDays, $excluded);
我传入开始日期、结束日期、有效日期所在的工作日数组以及要排除的日期数组。
如果我在这样的函数内print_r()
$next_day = new DateTime($date);
$next_day->add(new DateInterval('P1D'));
print_r($dates);
progDateRange($next_day->format('Y-m-d'), $end_date, $wkDays, $excluded, $dates);
每个循环都会打印出数组并继续成功添加,但由于某种原因,当我尝试在单独的页面上使用 print_r($datesArray) 时,没有任何输出,甚至没有一个空白数组,我根本不知道为什么.
我敢肯定这会很愚蠢,因为该函数似乎在大多数情况下都可以工作,只是在返回数据时遇到了问题。
我错过了什么?
我也刚刚尝试在 return 语句之前执行print_r(),这将返回我想要得到的确切数组。调用函数的页面上数据的返回/检索肯定有问题...
编辑
正如我之前没有提到的,这里是 $wkDays 和 $excluded 的示例 var 转储
$wkDays 产生
array(6) {
[0]=>
string(6) "Monday"
[1]=>
string(7) "Tuesday"
[2]=>
string(9) "Wednesday"
[3]=>
string(8) "Thursday"
[4]=>
string(6) "Friday"
[5]=>
string(6) "Sunday"
}
和$excludes 可能是这样的
array(23) {
[0]=>
string(10) "2013-04-22"
[1]=>
string(10) "2013-04-29"
[2]=>
string(10) "2013-05-13"
[3]=>
string(10) "2013-05-27"
[4]=>
string(10) "2013-06-03"
//...
}
一个示例调用可能是这样的;
progDateRange("2013-05-01", "2017-05-01", array("Monday", "Wednesday"), array("2013-06-12", "2013-06-19"));
解决方案
以 Jacks 为例,我不得不做一些调整,最终得到了这个;
function progDateRange($date, $end_date, $wkDays, $excluded)
{
$dates = array();
$todays_date = strtotime(date("Y-m-d"));
$current_date = strtotime($date);
$max_date = min(strtotime('+2 years'), strtotime($end_date));
while ($current_date < $max_date)
{
if (!in_array($date, $excluded) && in_array(date('l', $current_date), $wkDays) && $current_date > $todays_date) {
array_push($dates, $date);
}
$current_date = strtotime('+1 day', $current_date);
$date = date('Y-m-d', $current_date);
}
return $dates;
}
【问题讨论】:
-
$max_date = date('Y-m-d', strtotime('+2 years'))看起来比你现在做的要容易得多:) 为什么递归?