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 关注
- 114 浏览
添加回答
举报