我需要为我正在处理的项目创建一个函数:
给出3个变量:
返回日期
即/
$start_date = "01-01-2011"
$days[] = [[Monday],[Wednesday],[Friday]]
$number_dates = 9
应该返回:
$dates[] = [[03-01-2011],[05-01-2011],[07-01-2011],[10-01-2011],[12-01-2011],[14-01-2011],[17-01-2011],[19-01-2011],[21-01-2011]]
我怎样才能以最简单的方式实现这一目标
答案 0 :(得分:1)
$start_date = "01-01-2011";
$days = array('Monday', 'Wednesday', 'Friday');
$number_dates = 9;
$result = getDates($start_date, $days, $number_dates);
echo "<pre>";echo var_dump($result);echo "</pre>";
function getDates($start_date, $days, $number_dates) {
$result = array();
$startTime = mktime(0,0,0, (int)substr($start_date,4,2), (int)substr($start_date,0,2), (int)substr($start_date,6,4));
if (count($days) > 0) {
$n = 1;
$t = $startTime;
while ($n <= $number_dates) {
$t += 24 * 3600;
if (in_array(date('l', $t), $days)) {
$result[] = date('d-m-Y', $t);
$n++;
}
}
}
return $result;
}
结果:
<pre>array(9) {
[0]=>
string(10) "03-01-2011"
[1]=>
string(10) "05-01-2011"
[2]=>
string(10) "07-01-2011"
[3]=>
string(10) "10-01-2011"
[4]=>
string(10) "12-01-2011"
[5]=>
string(10) "14-01-2011"
[6]=>
string(10) "17-01-2011"
[7]=>
string(10) "19-01-2011"
[8]=>
string(10) "21-01-2011"
}
</pre>