douqin7086 2015-07-25 11:08
浏览 65
已采纳

PHP获取包含开始日期和结束日期的年,月和日期的数组

I'm stuck in a PHP function: given a start date (ie. 2011-11-25) and an end date (ie. 2011-03-18), I'd like to get an array containing years > months > dates like this:

Array
(
    [2010] => Array
        (
            [11] => Array
                (
                    [0] => 25
                    [1] => 26
                    [2] => 27
                    [3] => 28
                    [4] => 29
                    [5] => 30
                )

            [12] => Array
                (
                    [0] => 01
                    [1] => 02
                    [2] => 03
                    [3] => 04
                    [4] => 05
                    [5] => 06
                    [6] => 07
                    [7] => 08
                    [8] => 09
                    [9] => 10
                    [10] => 11
                    [11] => 12
                    [12] => 13
                    [13] => 14
                    [14] => 15
                    [15] => 16
                    [16] => 17
                    [17] => 18
                    [18] => 19
                    [19] => 20
                    [20] => 21
                    [21] => 22
                    [22] => 23
                    [23] => 24
                    [24] => 25
                    [25] => 26
                    [26] => 27
                    [27] => 28
                    [28] => 29
                    [29] => 30
                    [30] => 31
                )

        )

    [2011] => Array
        (
            [01] => Array
                (
                    [0] => 01
                    [1] => 02
                    [2] => 03
                    [3] => 04
                    [4] => 05
                    [5] => 06
                    [6] => 07
                    [7] => 08
                    [8] => 09
                    [9] => 10
                    [10] => 11
                    [11] => 12
                    [12] => 13
                    [13] => 14
                    [14] => 15
                    [15] => 16
                    [16] => 17
                    [17] => 18
                    [18] => 19
                    [19] => 20
                    [20] => 21
                    [21] => 22
                    [22] => 23
                    [23] => 24
                    [24] => 25
                    [25] => 26
                    [26] => 27
                    [27] => 28
                    [28] => 29
                    [29] => 30
                    [30] => 31
                )

            [02] => Array
                (
                    [0] => 01
                    [1] => 02
                    [2] => 03
                    [3] => 04
                    [4] => 05
                    [5] => 06
                    [6] => 07
                    [7] => 08
                    [8] => 09
                    [9] => 10
                    [10] => 11
                    [11] => 12
                    [12] => 13
                    [13] => 14
                    [14] => 15
                    [15] => 16
                    [16] => 17
                    [17] => 18
                    [18] => 19
                    [19] => 20
                    [20] => 21
                    [21] => 22
                    [22] => 23
                    [23] => 24
                    [24] => 25
                    [25] => 26
                    [26] => 27
                    [27] => 28
                )

            [03] => Array
                (
                    [0] => 01
                    [1] => 02
                    [2] => 03
                    [3] => 04
                    [4] => 05
                    [5] => 06
                    [6] => 07
                    [7] => 08
                    [8] => 09
                    [9] => 10
                    [10] => 11
                    [11] => 12
                    [12] => 13
                    [13] => 14
                    [14] => 15
                    [15] => 16
                    [16] => 17
                    [17] => 18
                )

        )

)

Googling around I found a function to get an array containing years > months:

function get_months($startstring, $endstring) {
    $time1 = strtotime($startstring);
    $time2 = strtotime($endstring);
    $year1 = date('Y', $time1);
    $year2 = date('Y', $time2);
    $years = range($year1, $year2);

    foreach ($years as $year) {
        $months[$year] = array();
        while ($time1 < $time2) {
            if (date('Y', $time1) == $year){
                $month = date('m', $time1);
                $months[$year][$month] = array();

                $time1 = strtotime(date('Y-m', $time1).' +1 month');
            } else {
                break;
            }
        }
    }
    return $months;
}

// ----------------------------------------------------------------

foreach(array_keys($montharr) as $year){
    foreach($montharr[$year] as $month){
        print "{$year}-{$month}<br>";
    }
}

But I have no idea how implementing days too

Please, any help?

  • 写回答

1条回答 默认 最新

  • douyou3619 2015-07-25 11:35
    关注

    You can use the following function:

    <?php
    function generateDates($start, $end)
    {
      $result = [];
    
      while ($start <= $end) {
        $result[$start->format('Y')][$start->format('m')][] = $start->format('d');
        $start->add(new DateInterval('P1D'));
      }
    
      return $result;
    }
    
    $start = new DateTime('2010-11-03');
    $end = new DateTime('2012-01-15');
    
    print_r(generateDates($start, $end));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记