如何获取两个月/年点之间的每个月数和年份

我正在创建一个函数,您可以在其中给出起始年、月和结束年、月,并且该函数将在两个给定点之间的每个月和年打印。


我已经创建了一个可以完美运行的函数,但我相信这不是一个好的做法,可能有更好的方法来做到这一点。现在我正在寻求您的帮助以找到更好的方法。


PS 我无法获取完整日期作为输入。只能获取月份和年份。


这是我的代码 -


function get_all_months($monthstart = null, $yearstart = null, $monthend = null, $yearend = null) {

if (($monthstart === null) || ($yearstart === null) || ($monthend === null) || ($yearend === null)) {

    $monthend = date('m');

    $yearend = date('Y');

    if($monthend < 6) {

        $yearstart = $yearend - 1;

        $monthstart = (($monthend - 5) + 12);

    } else {

        $yearstart = $yearend;

        $monthstart = $monthend - 5;

    }

}

$month_array = array();

if ($yearstart > $yearend) {

    for ($m=$monthend; $m<=12; $m++) $month_array[] = array('month' => $m, 'year' => $yearend);

    for ($y=$yearend+1; $y<$yearstart; $y++) for ($m=1; $m<=12; $m++) $month_array[] = array('month' => $m, 'year' => $y);

    for ($m=1; $m<=$monthstart; $m++) $month_array[] = array('month' => $m, 'year' => $yearstart);

} elseif ($yearend > $yearstart) {

    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);

} else {

    for ($m=$monthstart; $m<=$monthend; $m++) $month_array[] = array('month' => $m, 'year' => $yearstart);

}

return $month_array;

}

编辑:根据 Nigel Ren 的回答,这是我能想到的最好的方法 -


 function get_all_months($monthstart = null, $yearstart = null, $monthend = null, $yearend = null) {


if (($monthstart === null) || ($yearstart === null) || ($monthend === null) || ($yearend === null)) {

    $monthend = date('m');

    $yearend = date('Y');

    if($monthend < 6) {

        $yearstart = $yearend - 1;

        $monthstart = (($monthend - 5) + 12);

    } else {

        $yearstart = $yearend;

        $monthstart = $monthend - 5;

    }

}


绝地无双
浏览 115回答 5
5回答

牛魔王的故事

您可以使用 PHP 的DateInterval类来实现这些目的。试试这个代码<?phpfunction 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 )

胡说叔叔

只需在开始日期上添加 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)

墨色风雨

也许你正在寻找这样的东西:function get_all_months($monthstart = null, $yearstart = null, $monthend = null, $yearend = null) {&nbsp; &nbsp; $month_array = [];&nbsp; &nbsp; if ($yearstart == $yearend)&nbsp; &nbsp; &nbsp; &nbsp; for ($m=$monthstart; $m<=$monthend; $m++) $month_array[] = array('month' => $m, 'year' => $yearstart);&nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; for ($m=$monthstart; $m<=12; $m++) $month_array[] = array('month' => $m, 'year' => $yearstart);&nbsp; &nbsp; &nbsp; &nbsp; for ($y=$yearstart+1; $y<$yearend; $y++) for ($m=1; $m<=12; $m++) $month_array[] = array('month' => $m, 'year' => $y);&nbsp; &nbsp; &nbsp; &nbsp; for ($m=1; $m<=$monthend; $m++) $month_array[] = array('month' => $m, 'year' => $yearend);&nbsp; &nbsp; }&nbsp; &nbsp; return $month_array;}

HUWWW

使用DateTime类和DateInterval,您可以创建指定范围的 DateTime 对象的生成器:<?phpfunction 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 应该比普通字符串或数字数组更灵活地使用

函数式编程

如果我理解正确的话 - 你会得到数字形式的月份和数字形式的年份,例如 6 和 1997。如果我们假设开始年份总是小于结束年份,我建议这样做。function distanceBetweenDates(int $sm, int $sy, int $em, int $ey) {&nbsp; &nbsp; $monthsBetweenYears = ($ey - $sy + 1) * 12;&nbsp; &nbsp; $distanceBetweenMonths = $monthsBetweenYears - $sm - (12 - $em);&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; $startMonth = $sm + 1;&nbsp; &nbsp; $startYear = $sy;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; while ($distanceBetweenMonths > 0) {&nbsp; &nbsp; &nbsp; &nbsp; if ($startMonth <= 12) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo $startMonth . ' - ' . $startYear;&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $startMonth = 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $startYear++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo $startMonth . ' - ' . $startYear;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; echo "\n";&nbsp; &nbsp; &nbsp; &nbsp; $startMonth++;&nbsp; &nbsp; &nbsp; &nbsp; $distanceBetweenMonths--;&nbsp; &nbsp; }}您可能需要查看的唯一事情是计算中是否包含或排除给定的月份。这里唯一缺少的是验证,因为如果您在方法内使用类型,则可以“跳过”某种验证。
打开App,查看更多内容
随时随地看视频慕课网APP