我有两个日期,即事件开始日期和结束日期,在我的php脚本中,我想找出两个日期之间所有可能的日期组合
我的意思是我想找出参与者参加活动的所有可能日子,
假设开始日期为2014年10月10日,结束日期为2014年10月23日,
所有可能的组合都是
我想找出所有可能的组合的原因是,我根据活动参与者日会议有不同的折扣选项。
答案 0 :(得分:3)
这个问题可以分为两部分:
对于第一部分,我将使用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
)
)