PHP循环日期(按月)

时间:2018-07-12 20:29:07

标签: php arrays loops date

在此日期数组上有一个PEBKAC ...我不知道为什么第二维的第一个位置会被日期的最后一次修改所覆盖...所有帮助非常感谢!

$year=date('Y');
$count=date('m'); //loop from begining of current year to current month

$begin =new DateTime("$year-01-01");
$end = clone $begin;


for ($i=0; $i < $count; $i++){
    if ($i >0){ 
        $begin=$begin->add(new DateInterval('P1M'));
        $end = clone $begin;
    }
    $dateArray[$i][0]=$begin;
    $dateArray[$i][1]=$end->modify('last day of this month');
}

我正在寻找的输出是一个多维数组,该数组具有从当前年份到当前月份的每个月的第一个日期和最后一个日期。

2 个答案:

答案 0 :(得分:1)

您要存储对$begin的引用,然后在每个循环上更新$begin。将$begin分配给数组时,请对其进行克隆:

$dateArray[$i][0] = clone $begin;

答案 1 :(得分:0)

$year  =date('Y');
$count =date('m'); //loop from begining of current year to current month

$begin = new DateTime("$year-01-01");
$end   = clone $begin;

for ($i=0; $i < $count; $i++){
    if ($i >0){ 
        $begin = $begin->add(new DateInterval('P1M'));
        $end   = clone $begin;
    }
    $dateArray[$i][0]=clone $begin;
    $dateArray[$i][1]=$end->modify('last day of this month');
}

会产生:

Array
(
[0] => Array
    (
        [0] => DateTime Object
            (
                [date] => 2018-01-01 00:00:00.000000
                [timezone_type] => 3
            )

        [1] => DateTime Object
            (
                [date] => 2018-01-31 00:00:00.000000
                [timezone_type] => 3
            )
    )
[1] => Array
    (
        [0] => DateTime Object
            (
                [date] => 2018-02-01 00:00:00.000000
                [timezone_type] => 3
            )

        [1] => DateTime Object
            (
                [date] => 2018-02-28 00:00:00.000000
                [timezone_type] => 3
            )
    )
[2] => Array
    (
        [0] => DateTime Object
            (
                [date] => 2018-03-01 00:00:00.000000
                [timezone_type] => 3
            )
        [1] => DateTime Object
            (
                [date] => 2018-03-31 00:00:00.000000
                [timezone_type] => 3
            )
    )
[3] => Array
    (
        [0] => DateTime Object
            (
                [date] => 2018-04-01 00:00:00.000000
                [timezone_type] => 3
            )
        [1] => DateTime Object
            (
                [date] => 2018-04-30 00:00:00.000000
                [timezone_type] => 3
            )
    )
[4] => Array
    (
        [0] => DateTime Object
            (
                [date] => 2018-05-01 00:00:00.000000
                [timezone_type] => 3
            )
        [1] => DateTime Object
            (
                [date] => 2018-05-31 00:00:00.000000
                [timezone_type] => 3
            )
    )
[5] => Array
    (
        [0] => DateTime Object
            (
                [date] => 2018-06-01 00:00:00.000000
                [timezone_type] => 3
            )
        [1] => DateTime Object
            (
                [date] => 2018-06-30 00:00:00.000000
                [timezone_type] => 3
            )
    )
[6] => Array
    (
        [0] => DateTime Object
            (
                [date] => 2018-07-01 00:00:00.000000
                [timezone_type] => 3
            )
        [1] => DateTime Object
            (
                [date] => 2018-07-31 00:00:00.000000
                [timezone_type] => 3
            )
    )
)