2014-09-22 49 views
1

内的所有可能的日期组合我有两个日期,这是事件的开始日期和结束日期,在我的PHP脚本我想找出这两个日期PHP查找间隔

之间我的意思是我日期的所有可能的组合要找出所有可能的天参与者来一个事件,

假设开始日期IN20/10/2014年和结束日期is23/10/2014,

所有可能的组合是

  1. 20/10/2014 -23/10/2014
  2. 20/10/2014 -22/10/2014
  3. 20/10/2014 -21/10/2014
  4. 21/10/2014 -23 /二千零十四分之十
  5. 21/10/2014 -22/10/2014
  6. 22/10/2014 -23/10/2014
  7. 20/10/2014 -21/10/2014- 22/10/2014
  8. 20/10/2014 -21/10/2014- 22/10/2014 -23/10/2014
  9. 21/10/2014 -22/10/2014 -23/10/2014

我想找出所有可能的组合的原因是,我有基于活动参与者日会话的不同折扣选项。

+0

你应该使用不同的列'开始date'和'结束date'在表 – Manwal 2014-09-22 09:16:55

+0

让我们了解您已经做了 – niyou 2014-09-22 09:28:52

+0

怎么了用简单的复选框:第1天,第2天等?单日参与情况如何?或者例如20/10/2014和23/10/2014(跳过一两天)? – 2014-09-22 11:12:44

回答

3

这个问题可以在两个部分被分解:

  1. 对于任何两个日期,创建具有所有落入日期间隔内的天的阵列。
  2. 创建所有可能天数的所有可能组合(从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 
     ) 

)