2016-05-31 94 views
0

我有一个PHP内循环,应该得到过去的12个完整月份。今天,5月31日,这些月份的标签似乎重复。使用PHP日期与strtotime循环几个月创建重复

三月取代二月 月取代十一月 十月取代九月

$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

如果你从5月31日减去1个月,你会有什么期望.....应该PHP告诉你4月31日? –

+1

[如何获取上一个月和今年相对于今天,使用strtotime和日期?]可能重复(http://stackoverflow.com/questions/5489502/how-to-get-previous-month-and-year-relative到今天使用strtotime和日期) –

+0

@MarkBaker我希望PHP找到上个月的最后一个日期,4月30日。同样从3月31日减去1个月,我预计会看到2月28日/ 29日。 1 month!=本月中的天数 –

回答

3

编程方式确定之日起N个月,在过去或未来是一个不平凡的问题,当今天的月份(31)日在有关月份并不存在。是1月31日后的1个月:2月28日还是3月2日?显然,人类知道它取决于“1个月”的含义,但PHP选择后者。

您最安全的赌注是要首先将您的起始日期设置为当月的第一天。

for ($index = 0, $date = new DateTime("first day of this month", new DateTimeZone("America/Chicago")); 
    ++$index <= 12; $date->modify("-1 month")) { 
    $dates[] = [ 
     "query_year" => $date->format("Y"), 
     "query_month" => $date->format("m"), 
     "display_month" => $date->format("M") 
    ]; 
} 

或者,使用替代DateTime类PHP的date功能:

for ($index = 0, $date = strtotime("first day of this month America/Chicago"); 
    ++$index <= 12; $date = strtotime("-1 month", $date)) { 
    $dates[] = [ 
     "query_year" => date("Y", $date), 
     "query_month" => date("m", $date), 
     "display_month" => date("M", $date) 
    ]; 
} 
1
<?php 

$date = new DateTime('first day of this month', new DateTimeZone('America/Chicago')); 
$dates = []; 
for($i = 12; $i>0;$i--) { 
    $dates[] = [ 
     'query_year' => $date->format('Y'), 
     'query_month' => $date->format('m'), 
     'display_month' => $date->format('M'), 
    ]; 
    $date->sub(new DateInterval("P1M")); 
} 
var_dump($dates); 
+0

这是什么意思? $ date-> sub(new DateInterval(“P1M”)); –

+0

'P1M'是'1个月'的iso标准周期表示; –

+0

P - 期间,1M - 1个月 – Pyton

1

与您的代码的问题是,你必须使用每月的第一天为起点。有些月份有31天,有些月份有30天或29天。字符串“-1个月”中的减少日数基于上个月而非当前月份。

<?php 
     date_default_timezone_set("America/Chicago"); 
     $count = 0; 
     $data_array = array(); 
     $year_month = date("Y-m"); 
     $first_day = strtotime("$year_month-01 00:00:00"); 
     while ($count < 12) { 
      $query_year = date("Y", strtotime("-".$count." months", $first_day)); 
      $query_month = date("m", strtotime("-".$count." months", $first_day)); 
      $display_month = date("M", strtotime("-".$count." months", $first_day)); 
      $date_array[] = array(
       "query_year" => $query_year, 
       "query_month" => $query_month, 
       "display_month" => $display_month 
      ); 
      $count++; 
     } 
     print_r($date_array); 
    ?> 
+1

请添加文字说明你的代码在做什么,而不是只是倾销所有的代码作为答案。 –

+0

@HunterTurner感谢您的建议。我在使用英语时遇到了一些困难,但我会尽我所能。 – w169q169