strtotime“-1 月”的意外日期结果

Unexpected date outcome for strtotime "-1 month"

我正在处理一组统计数据,当我发现某些日期产生了意外结果时,我需要指定给定任何开始日期的前一个月的开始:

$theday = '2015-12-31';
for ($i = 1; $i < 6; $i++) {
    echo "minus $i month = " . date('Y-m', strtotime ("-$i month" , strtotime ( $theday )) ) . '-01' . "\n";
}

输出:

minus 1 month = 2015-12-01
minus 2 month = 2015-10-01
minus 3 month = 2015-10-01
minus 4 month = 2015-08-01
minus 5 month = 2015-07-01

我哪里做错了?获取日期 X 前一个月的年月的正确方法是什么?

根据https://derickrethans.nl/obtaining-the-next-month-in-php.html,可以写成"first day of -x month"。以下代码:

$theday = '2015-12-02';
for ($i = 1; $i < 6; $i++) {
    echo "minus $i month = " . date('Y-m-d', strtotime ("first day of -$i month" , strtotime ( $theday )) ) . "\n";
}

结果:

minus 1 month = 2015-11-01
minus 2 month = 2015-10-01
minus 3 month = 2015-09-01
minus 4 month = 2015-08-01
minus 5 month = 2015-07-01