PHP根据用户选择生成具有月和年数的数组

时间:2018-03-29 07:09:46

标签: php arrays

我有以下四种形式的要求:

  

Month_From:11
  Year_From:2015年   Month_To:3
  Year_To:2016

现在我需要从用户选择的数据值生成一个包含所有月份和年份的数组:

Array (
[0] = Array(
   'month'   => '11'
   'year'    => '2015'
 );
[1] = Array(
   'month'   => '12'
   'year'    => '2015'
 );

[2] = Array(
   'month'   => '1'
   'year'    => '2016'
 );

[3] = Array(
   'month'   => '2'
   'year'    => '2016'
 );
[4] = Array(
   'month'   => '3'
   'year'    => '2016'
 );
);

5 个答案:

答案 0 :(得分:3)

您可以尝试使用以下代码。我在整个时间框架内进行了迭代,并使用每个月创建一个数组。

$start    = (new DateTime('2015-11-01'))->modify('first day of this month');
$end      = (new DateTime('2016-03-01'))->modify('first day of next month');
$interval = DateInterval::createFromDateString('1 month');
$period   = new DatePeriod($start, $interval, $end);
$data = array();

foreach ($period as $dt) {
    $data_month = array(
        'month' => $dt->format("m"),
        'year' => $dt->format("Y")
  );
  array_push($data, $data_month);
}
print_r($data);

答案 1 :(得分:1)

您可以执行2个while循环,例如:

$Month_From = 11;
$Year_From = 2015;
$Month_To = 3;
$Year_To = 2016;

$result = array();
$tMonth = $Month_From;
$tYear = $Year_From;

while( $tYear <= $Year_To ) {
    while( ( $tMonth <= 12 && $tYear < $Year_To ) || ( $tMonth <= $Month_To && $tYear == $Year_To ) ) {
        $result[] = array(
            "month" => $tMonth,
            "year" => $tYear,
        );
        $tMonth++;
    }

    $tMonth = 1;
    $tYear++;
}

echo "<pre>";
print_r( $result );
echo "</pre>";

这将导致:

Array
(
    [0] => Array
        (
            [month] => 11
            [year] => 2015
        )

    [1] => Array
        (
            [month] => 12
            [year] => 2015
        )

    [2] => Array
        (
            [month] => 1
            [year] => 2016
        )

    [3] => Array
        (
            [month] => 2
            [year] => 2016
        )

    [4] => Array
        (
            [month] => 3
            [year] => 2016
        )

)

答案 2 :(得分:0)

您可以根据日期输入建立日期。

我没有给你确切的代码。但只是要提示如何归档解决方案。

$start_date = sprintf('%s-%s-1', $start_year, $start_month);

$end_date = sprintf('%s-%s-1', $end_year, $end_month);


while (strtotime($date) <= strtotime($end_date)) {

    $next_date = date("Y-m-d", strtotime("first day of next month", strtotime($date)));

}

答案 3 :(得分:0)

以下是使用for循环执行此操作的另一种方法。

    $m1 = 11;        $y1 = 2015;        $m2 = 3;        $y2 = 2016;

    $mlist = array();
    $diff = (($y2 - $y1) * 12) + ($m2 - $m1);

    for($i=0;$i<=$diff;$i++){
        $mlist[] = array('month'=>$m1,'year'=>$y1);
        $m1++;
        if($m1 > 12){
            $m1 = $m1-12;
            $y1++;
        }
    }

    echo "<pre>";
    print_r($mlist);
    echo "</pre>";

答案 4 :(得分:0)

您只能使用数组函数范围

//months 1-12

$fromMonth = 11;
$fromYear = 2015;
$toMonth = 3;
$toYear = 2016;

$years = range($fromYear, $toYear);
foreach($years as $year) {

  $months = range(1, 12);

  if($year==$fromYear) {
    $months = range($fromMonth, 12);
  }
  elseif($year==$toYear) {
    $months = range(1, $toMonth);
  }

  foreach($months as $month) {
    $ret[] = [
        'year' => $year,
        'month' => $month,
    ];
  }
}