簡體   English   中英

PHP | 獲取兩個重疊日期范圍的總月份數

[英]PHP | Get total Months of two Overlapping Date Ranges

我想弄清楚如何在兩個重疊的日期范圍之間獲得總月份數。

例如,從日期-A 到日期-B 的日期范圍與日期-X 和日期-Y 的日期范圍重疊。

_ Start|Jan - Feb - March - April ------ Nov - Dec|End (DateRange A) Start|Jan - Feb ---- Dec - Jan - Feb|End _

在兩個日期范圍內,一月和二月發生沖突。 意味着總共有 2 個月。 所以我希望這兩個月在我的數組中,這樣我就可以在它上面應用不同的功能。

例如我有這兩個日期

$dateRange1Start = "2015-07-01";
$dateRange1End = "2014-06-30"; 
=-=-=-=====-=-==
$dateRange2Start = "2012-02-01";
$dateRange2End = "2014-12-31";

這兩個日期范圍之間總共有 6 個月的時間沖突。 我想要這 6 個月。

我試圖在谷歌搜索幫助,但大多數情況下我得到的結果小於或大於符號。 但特別喜歡這個。 我試圖實現我自己的邏輯,但它沒有讓我在任何地方,可悲的是只有更多的問題:(

我試圖得到這樣的結果

$collidingDates = array("2014-07","2014-08","2014-09","2014-10","2014-11","2014-12");

任何幫助,將不勝感激。 :)

==-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=--=-=

更新:

這是我到目前為止所做的,是的,從這里和那里獲取一些代碼。 並稍微調整一下以滿足我的要求。

//Getting all Months from First Date Range

$start = (new DateTime('2014-06-01'))->modify('first day of this month');

 $end = (new DateTime('2015-05-06'))->modify('first day of next month');

 $interval = DateInterval::createFromDateString('1 month'); 

$firstPeriod = new DatePeriod($start, $interval, $end);



//Getting all Months from Second Date Range.
$start = (new DateTime('2012-02-01'))->modify('first day of this month');

 $end = (new DateTime('2014-12-31'))->modify('first day of next month');

 $interval = DateInterval::createFromDateString('1 month'); 

$secondPeriod = new DatePeriod($start, $interval, $end);


$collidingDates = array();

 foreach ($firstPeriod as $f_dt) { 
   foreach($secondPeriod as $s_dt){
        if($f_dt->format("Y-m") ===  $s_dt->format("Y-m")){

            array_push($collidingDates,$f_dt->format("Y-m"));

        }

   }

 }

echo "<pre>";
print_r($collidingDates);

我得到了這個輸出。

Array
    (
        [0] => 2014-06
        [1] => 2014-07
        [2] => 2014-08
        [3] => 2014-09
        [4] => 2014-10
        [5] => 2014-11
        [6] => 2014-12
    )

但我想我會在 2014-06 年多 1 個月,不知道 O_o 怎么樣??

這是一個兩步過程。 首先,您需要從開始日期和結束日期確定最窄的日期范圍。 然后列出這些日期之間的月份。

// An array of start and end dates. There are just 2 in this example but you
// could have as many as you like in the same "start, then end" format.
$ranges = [
    [new DateTime('2014-07-01'), new DateTime('2015-06-30')],
    [new DateTime('2012-02-01'), new DateTime('2014-12-31')]
];

// Reduce the ranges to one set of two dates, the latest of the start dates
// and the earliest of the end dates.
$range = array_reduce($ranges, function($carry, $item){
    return $carry
        ? [max($carry[0], $item[0]), min($carry[1], $item[1])]
        : $item;
});

var_dump($range);
/*
array (size=2)
  0 => 
    object(DateTime)[1]
      public 'date' => string '2014-07-01 00:00:00.000000' (length=26)
      public 'timezone_type' => int 3
      public 'timezone' => string 'Europe/London' (length=13)
  1 => 
    object(DateTime)[4]
      public 'date' => string '2014-12-31 00:00:00.000000' (length=26)
      public 'timezone_type' => int 3
      public 'timezone' => string 'Europe/London' (length=13)
 */

// Shift both dates to the first of the month. Strictly speaking, we only
// need to do this with the start date.
$range = array_map(function($date){
    return $date->modify("first day of this month");
}, $range);

var_dump($range);
/*
array (size=2)
  0 => 
    object(DateTime)[1]
      public 'date' => string '2014-07-01 00:00:00.000000' (length=26)
      public 'timezone_type' => int 3
      public 'timezone' => string 'Europe/London' (length=13)
  1 => 
    object(DateTime)[4]
      public 'date' => string '2014-12-01 00:00:00.000000' (length=26)
      public 'timezone_type' => int 3
      public 'timezone' => string 'Europe/London' (length=13)
 */

$months = [];
$interval = new DateInterval("P1M");
for ($month = $range[0]; $month <= $range[1]; $month->add($interval)) {
    $months[] = $month->format("Y-m");
}
var_dump($months);
/*
array (size=6)
  0 => string '2014-07' (length=7)
  1 => string '2014-08' (length=7)
  2 => string '2014-09' (length=7)
  3 => string '2014-10' (length=7)
  4 => string '2014-11' (length=7)
  5 => string '2014-12' (length=7)
 */

我想你正在尋找這個:

 $start = (
                new DateTime('2015-12-02'))
                ->modify('first day of this month');

 $end = (new DateTime('2016-05-06'))->modify('first day of next month');

 $interval = DateInterval::createFromDateString('1 month'); 

$period = new DatePeriod($start, $interval, $end);

 foreach ($period as $dt) { 

echo $dt->format("Y-m") . "<br>\n";

 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM