【发布时间】:2023-03-11 04:51:01
【问题描述】:
我在 PHP 中有一个循环,它应该得到过去 12 个月的完整时间。今天,5 月 31 日,那些月份的标签似乎重复了。
3 月取代 2 月 十二月取代十一月 10 月取代 9 月
$count = 1;
$date_array = array();
while ($count <= 12) {
// Calculate date
$query_year = date('Y', strtotime('-' . $count . ' months America/Chicago'));
$query_month = date('m', strtotime('-' . $count . ' months America/Chicago'));
$display_month = date('M', strtotime('-' . $count . ' months America/Chicago'));
$date_array[] = array(
'query_year' => $query_year,
'query_month' => $query_month,
'display_month' => $display_month
);
$count ++;
}
print_r($date_array);
下面是实际代码:http://ideone.com/dBklOH
知道为什么会这样吗?它从今天 5 月 31 日才开始,可能会在明天自行解决。
【问题讨论】:
-
如果你从 5 月 31 日减去 1 个月,你期望什么..... PHP 应该告诉你 4 月 31 日吗?
-
@MarkBaker 我希望 PHP 能够找到上个月的最后一个日期,即 4 月 30 日。与从 3 月 31 日减去 1 个月相同,我预计会在 2 月 28 日/29 日看到。 1 个月 != 本月的天数
-
@JeffWalden - 那么您的期望是错误的,正如马特的回答中所解释的那样...... PHP 有效地减少了日期 2016-03-31 的月份部分以给出 2016-02-31(调整通过减少第 1 个月的年份,但不调整日期....如果您从计算机的角度考虑,这非常合乎逻辑
-
这是非常有据可查的行为,已经多次被问及回答。
标签: php