日历数组上个月

时间:2017-05-12 16:54:43

标签: php arrays calendar

所以我创建了一个日历应用程序来放入一个网站,它本应该:

  • 显示当月
  • 显示下一个(3)个月
  • 显示前2个月
  • 显示上个月的结束和下个月的开始(如果布局允许的话)(就像Windows日历一样。)

所以现在我正在我的代码生成一个包含所有数据的数组,它自动使它从正确的位置开始(根据每月的第一天)并设置默认数量到了42天。(因为那是Windows如何做到的。)

这是数组输出:https://pastebin.com/NqLzNW5Z 这是我的Calendar.class.php文件:https://pastebin.com/Hin8q7xW(有些话是荷兰语,请原谅。)

我的问题:如何更改以下内容:

        [7] => Array
            (
                [0] => Before Month Start
                [1] => Before Month Start
                [2] => Before Month Start
                [3] => 01-06-2017
                [4] => 02-06-2017
                [5] => 03-06-2017
                [6] => 04-06-2017
                [7] => 05-06-2017
                [8] => 06-06-2017
                [9] => 07-06-2017
                [10] => 08-06-2017
                [11] => 09-06-2017
                [12] => 10-06-2017
                [13] => 11-06-2017
                [14] => 12-06-2017
                [15] => 13-06-2017
                [16] => 14-06-2017
                [17] => 15-06-2017
                [18] => 16-06-2017
                [19] => 17-06-2017
                [20] => 18-06-2017
                [21] => 19-06-2017
                [22] => 20-06-2017
                [23] => 21-06-2017
                [24] => 22-06-2017
                [25] => 23-06-2017
                [26] => 24-06-2017
                [27] => 25-06-2017
                [28] => 26-06-2017
                [29] => 27-06-2017
                [30] => 28-06-2017
                [31] => 29-06-2017
                [32] => 30-06-2017
                [33] => After month
                [34] => After month
                [35] => After month
                [36] => After month
                [37] => After month
                [38] => After month
                [39] => After month
                [40] => After month
                [41] => After month
            )

对于这样的事情:

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

注意:能够看到的第一个月和最后一个月不必提前一个月和一个月后显示。因为这需要我在另一个月加载。只是为了这几天。

最终结果将与滑块放在一起。当用户点击下个月的箭头时,滑块将显示下个月。

如果有人知道如何帮我完成,请告诉我。如果有人知道更好的方法,也请告诉我!

感谢

1 个答案:

答案 0 :(得分:0)

这是未经测试的,如果您遇到任何问题,请告诉我。

    public function makeMonth($m, $y)
    {

        $maand = array();
        // Get info for this year
        $info = $this->getInfo($m, $y);

        // Get array with dates
        $amount = $info['days'];
        $dateStart = strtotime("{$y}-{$m}-1");
        for ($i = 1; $i <= $amount; $i++) {
            $maand[$i] = str_pad($i, 2, '0', STR_PAD_LEFT) . "-" . $m . "-" . $y;
            $dateEnd = strtotime("{$y}-{$m}-{$i}");
        }

        // place the array content correctly
        $needed = 42;
        $begin = $info['firstday'] - 1;
        $nognodig = $needed - ($begin + $amount);

        #begin van array
        while ($begin > 0) {
            $begin--;
            $dateStart = strtotime("-1 day", $dateStart);
            array_unshift($maand, date("d-m-Y", $dateStart));
        }

        #eind van array
        while ($nognodig > 0) {
            $nognodig--;
            $dateEnd = strtotime("+1 day", $dateEnd);
            array_push($maand, date("d-m-Y", $dateEnd));
        }
    }