5 回答

TA貢獻1830條經驗 獲得超3個贊
您可以使用 PHP 的DateInterval類來實現這些目的。試試這個代碼
<?php
function getMonthsFromRange($start, $end, $format = 'M Y')
{
? ? $array = array();
? ? // Since you wanted 1 month it is Period = 1 Month
? ? $interval = new DateInterval('P1M');
? ? $realEnd = new DateTime($end);
? ? $realEnd->add($interval);
? ? $period = new DatePeriod(new DateTime($start), $interval, $realEnd);
? ? // Use loop to store date into array?
? ? foreach ($period as $date)
? ? ? ? $array[] = $date->format($format);
? ? // Return the array elements?
? ? return $array;
}
// Function call with passing the start date and end date?
$months = getMonthsFromRange('2010-10', '2011-11');
print_r($months);
它的輸出是:
Array ( [0] => Oct 2010 [1] => Nov 2010 [2] => Dec 2010 [3] => Jan 2011 [4] => Feb 2011 [5] => Mar 2011 [6] => Apr 2011 [7] => May 2011 [8] => Jun 2011 [9] => Jul 2011 [10] => Aug 2011 [11] => Sep 2011 [12] => Oct 2011 [13] => Nov 2011 )

TA貢獻1804條經驗 獲得超8個贊
只需在開始日期上添加 1 個月直到結束日期即可完成...
function get_all_months($monthstart = null, $yearstart = null, $monthend = null, $yearend = null) {
? ? $output = [];
? ? $time? ?= strtotime($yearstart."-".$monthstart);
? ? $last? ?= date('m-Y', strtotime($yearend."-".$monthend));
? ? do {
? ? ? ? $month = date('m-Y', $time);
? ? ? ? $output[] =? $month;
? ? ? ? $time = strtotime('+1 month', $time);
? ? }
? ? while ($month != $last);
? ? return $output;
}
所以
print_r(get_all_months(4,2008,2,2010));
給...
Array
(
? ? [0] => 04-2008
? ? [1] => 05-2008
? ? [2] => 06-2008
? ? [3] => 07-2008
? ? [4] => 08-2008
? ? [5] => 09-2008
? ? [6] => 10-2008
? ? [7] => 11-2008
? ? [8] => 12-2008
? ? [9] => 01-2009
? ? [10] => 02-2009
? ? [11] => 03-2009
? ? [12] => 04-2009
? ? [13] => 05-2009
? ? [14] => 06-2009
? ? [15] => 07-2009
? ? [16] => 08-2009
? ? [17] => 09-2009
? ? [18] => 10-2009
? ? [19] => 11-2009
? ? [20] => 12-2009
? ? [21] => 01-2010
? ? [22] => 02-2010
)

TA貢獻1853條經驗 獲得超6個贊
也許你正在尋找這樣的東西:
function get_all_months($monthstart = null, $yearstart = null, $monthend = null, $yearend = null) {
$month_array = [];
if ($yearstart == $yearend)
for ($m=$monthstart; $m<=$monthend; $m++) $month_array[] = array('month' => $m, 'year' => $yearstart);
else {
for ($m=$monthstart; $m<=12; $m++) $month_array[] = array('month' => $m, 'year' => $yearstart);
for ($y=$yearstart+1; $y<$yearend; $y++) for ($m=1; $m<=12; $m++) $month_array[] = array('month' => $m, 'year' => $y);
for ($m=1; $m<=$monthend; $m++) $month_array[] = array('month' => $m, 'year' => $yearend);
}
return $month_array;
}

TA貢獻1874條經驗 獲得超12個贊
使用DateTime類和DateInterval,您可以創建指定范圍的 DateTime 對象的生成器:
<?php
function get_all_months(int $monthstart, int $yearstart, int $monthend, int $yearend) {
? ? ? ? $onemonth = new DateInterval('P1M'); // one month interval
? ? ? ? $timeperiod = new DatePeriod(
? ? ? ? ? ? ? ? DateTime::createFromFormat('Y-m', "{$yearstart}-{$monthstart}"),
? ? ? ? ? ? ? ? $onemonth,
? ? ? ? ? ? ? ? DateTime::createFromFormat('Y-m', "{$yearend}-{$monthend}")->add($onemonth)
? ? ? ? );
? ? ? ? foreach ($timeperiod as $pos) {
? ? ? ? ? ? ? ? yield clone $pos;
? ? ? ? }
}
foreach (get_all_months(12, 2009, 1, 2020) as $month) {
? ? ? ? echo "{$month->format('Y-m')}\n";
}
DateTime 應該比普通字符串或數字數組更靈活地使用

TA貢獻1807條經驗 獲得超9個贊
如果我理解正確的話 - 你會得到數字形式的月份和數字形式的年份,例如 6 和 1997。如果我們假設開始年份總是小于結束年份,我建議這樣做。
function distanceBetweenDates(int $sm, int $sy, int $em, int $ey) {
$monthsBetweenYears = ($ey - $sy + 1) * 12;
$distanceBetweenMonths = $monthsBetweenYears - $sm - (12 - $em);
$startMonth = $sm + 1;
$startYear = $sy;
while ($distanceBetweenMonths > 0) {
if ($startMonth <= 12) {
echo $startMonth . ' - ' . $startYear;
} else {
$startMonth = 1;
$startYear++;
echo $startMonth . ' - ' . $startYear;
}
echo "\n";
$startMonth++;
$distanceBetweenMonths--;
}
}
您可能需要查看的唯一事情是計算中是否包含或排除給定的月份。
這里唯一缺少的是驗證,因為如果您在方法內使用類型,則可以“跳過”某種驗證。
- 5 回答
- 0 關注
- 179 瀏覽
添加回答
舉報