1

私の質問では、「オフシーズン」または「デッド」が正しい定義であるかどうかはわかりません。とにかく、ここに説明があります:

私の配列には$period['from']とがあり、2013 年の「デッドまたはオフシーズン」期間を計算する必要があります。$period['to']

入力期間の例:

From            To
2013-04-01      2013-06-01
2013-07-15      2013-07-20
2013-09-01      2013-10-31

デッド期間は次のようになります (出力):

From            To
2013-01-01      2013-03-31
2013-06-02      2013-07-14
2013-07-21      2013-08-31
2013-11-10      2013-12-31

私はこれで完全に立ち往生しています。どんな助けでも大歓迎です、

カールス

4

1 に答える 1

2

これを試して:

function dateDiff(array $dates, $startAt = null, $endAt = null) {
    if ($startAt === null) {
        $startAt = date("Y-01-01");
        $start = strtotime($startAt) - 86400;
    } else {
        $start = strtotime($startAt);
    }        

    if ($endAt === null) {
      $endAt = date("Y-12-31");
    }

    $result = array();

    foreach ($dates as $row) {
        $to = strtotime($row['from']);      
        $result[] = array('from' => date('Y-m-d', $start + 86400), 'to' => date('Y-m-d', $to - 86400));
        $start = strtotime($row['to']);;
    }
    $result[] = array('from' => date('Y-m-d', $start + 86400), 'to' => date('Y-m-d', strtotime($endAt)));

    return $result;
}



$dates = array(
    array('from' => '2013-04-01', 'to' => '2013-06-01'),
    array('from' => '2013-07-15', 'to' => '2013-07-20'),
    array('from' => '2013-09-01', 'to' => '2013-10-31'),
);

print_r(dateDiff($dates));

これにより、次のものが生成されます。

Array ( 
    [0] => Array ( [from] => 2013-01-01 [to] => 2013-03-31 ) 
    [1] => Array ( [from] => 2013-06-02 [to] => 2013-07-14 ) 
    [2] => Array ( [from] => 2013-07-21 [to] => 2013-08-31 ) 
    [3] => Array ( [from] => 2013-11-01 [to] => 2013-12-31 ) 
)
于 2013-01-14T16:22:40.467 に答える