这个问题可以在两个部分被分解:
- 对于任何两个日期,创建具有所有落入日期间隔内的天的阵列。
- 创建所有可能天数的所有可能组合(从1到n,其中n ==间隔内的天数)。
对于第一部分,我将使用DatePeriod PHP类,并为第二个Math_Combinatorics PEAR包。下面是完整的代码:
require_once 'Math/Combinatorics.php';
date_default_timezone_set('UTC');
$format = "d/m/Y";
$start = DateTime::createFromFormat($format, "20/10/2014");
$end = DateTime::createFromFormat($format, "23/10/2014");
$period = new DatePeriod($start, new DateInterval('P1D'), $end);
$dates = array();
foreach ($period as $date) {
$dates[] = $date->format($format);
}
$dates[] = $end->format($format);
$combinations = array();
$combinatorics = new Math_Combinatorics();
foreach (range(1, count($dates)) as $number_of_combinations) {
foreach ($combinatorics->combinations($dates, $number_of_combinations) as $combination) {
$combinations[] = $combination;
}
}
print_r($combinations);
结果:
Array
(
[0] => Array
(
[0] => 20/10/2014
)
[1] => Array
(
[0] => 21/10/2014
)
[2] => Array
(
[0] => 22/10/2014
)
[3] => Array
(
[0] => 23/10/2014
)
[4] => Array
(
[0] => 20/10/2014
[1] => 21/10/2014
)
[5] => Array
(
[0] => 20/10/2014
[2] => 22/10/2014
)
[6] => Array
(
[0] => 20/10/2014
[3] => 23/10/2014
)
[7] => Array
(
[1] => 21/10/2014
[2] => 22/10/2014
)
[8] => Array
(
[1] => 21/10/2014
[3] => 23/10/2014
)
[9] => Array
(
[2] => 22/10/2014
[3] => 23/10/2014
)
[10] => Array
(
[0] => 20/10/2014
[1] => 21/10/2014
[2] => 22/10/2014
)
[11] => Array
(
[0] => 20/10/2014
[1] => 21/10/2014
[3] => 23/10/2014
)
[12] => Array
(
[0] => 20/10/2014
[2] => 22/10/2014
[3] => 23/10/2014
)
[13] => Array
(
[1] => 21/10/2014
[2] => 22/10/2014
[3] => 23/10/2014
)
[14] => Array
(
[0] => 20/10/2014
[1] => 21/10/2014
[2] => 22/10/2014
[3] => 23/10/2014
)
)
你应该使用不同的列'开始date'和'结束date'在表 – Manwal 2014-09-22 09:16:55
让我们了解您已经做了 – niyou 2014-09-22 09:28:52
怎么了用简单的复选框:第1天,第2天等?单日参与情况如何?或者例如20/10/2014和23/10/2014(跳过一两天)? – 2014-09-22 11:12:44