一年中每个月的php-for循环

一年中每个月的php-for循环,php,date,loops,for-loop,Php,Date,Loops,For Loop,我想要一个循环,检查当前月份、未来12个月和过去4个月 例如:今天是08年8月1日。我的循环应该经过四月、五月、六月、七月、八月、九月、十月、十一月、十二月、一月、二月、三月、四月、五月、六月、七月和八月 我已经尝试了一段时间,但我不知道我如何能在4个月前和12个月后循环 这是我的密码 $i = 1; $month = strtotime('2013-08-01'); while($i <= 12) { $month_name = date('F', $mont

我想要一个循环,检查当前月份、未来12个月和过去4个月

例如:今天是08年8月1日。我的循环应该经过四月、五月、六月、七月、八月、九月、十月、十一月、十二月、一月、二月、三月、四月、五月、六月、七月和八月

我已经尝试了一段时间,但我不知道我如何能在4个月前和12个月后循环

这是我的密码

$i = 1; 
$month = strtotime('2013-08-01');

    while($i <= 12) {
        $month_name = date('F', $month);
        echo $month_name;
        echo "<br>";

        $month = strtotime('+1 month', $month);
        $i++;
像这样的

$start = -4;
$end = 12;

for($i=$start; $i<=$end;$i++) {
    $month_name = date('F Y', strtotime("$i months"));
    echo $month_name;
    echo "<br>";
}
像这样的

$start = -4;
$end = 12;

for($i=$start; $i<=$end;$i++) {
    $month_name = date('F Y', strtotime("$i months"));
    echo $month_name;
    echo "<br>";
}

这可能会很棘手,我会这样做:

$month = date("n", "2013-08-01") - 1; // -1 to get 0-11 so we can do modulo

// since you want to go back 4 you can't just do $month - 4, use module trick:
$start_month = $month + 8 % 12;
// +8 % 12 is the same is -4 but without negative value issues
// 2 gives you: 2+8%12 = 10 and not -2

for ($i = 0; $i < 16; $i += 1) {
    $cur_month = ($start_month + $i) % 12 + 1; // +1 to get 1-12 range back
    $month_name = date('F Y', strtotime($cur_month . " months"));
    var_dump(month_name);
}

这可能会很棘手,我会这样做:

$month = date("n", "2013-08-01") - 1; // -1 to get 0-11 so we can do modulo

// since you want to go back 4 you can't just do $month - 4, use module trick:
$start_month = $month + 8 % 12;
// +8 % 12 is the same is -4 but without negative value issues
// 2 gives you: 2+8%12 = 10 and not -2

for ($i = 0; $i < 16; $i += 1) {
    $cur_month = ($start_month + $i) % 12 + 1; // +1 to get 1-12 range back
    $month_name = date('F Y', strtotime($cur_month . " months"));
    var_dump(month_name);
}
最简单的解决方案:

for($i=-4; $i<=12; $i++) {
    echo date("F",strtotime( ($i > 0) ? "+$i months" : "$i months") )."\n";
}
说明:

循环从-4开始,一直到12,总共17个,包括0。strotime中的三元语句只是检查$i是否为正,如果为正,则插入a+,这样我们就可以得到strotime+1个月和类似的结果

最简单的解决方案:

for($i=-4; $i<=12; $i++) {
    echo date("F",strtotime( ($i > 0) ? "+$i months" : "$i months") )."\n";
}
说明:

循环从-4开始,一直到12,总共17个,包括0。strotime中的三元语句只是检查$i是否为正,如果为正,则插入a+,这样我们就可以得到strotime+1个月和类似的结果


您的代码,只需稍加修改

date_default_timezone_set('UTC');

$i = 1;
$month = strtotime('-4 month');

while($i <= 16) {
    $month_name = date('F', $month);
    echo $month_name;
    echo "<br>";

    $month = strtotime('+1 month', $month);
    $i++;
}

你的代码,只是稍微修改了一下

date_default_timezone_set('UTC');

$i = 1;
$month = strtotime('-4 month');

while($i <= 16) {
    $month_name = date('F', $month);
    echo $month_name;
    echo "<br>";

    $month = strtotime('+1 month', $month);
    $i++;
}

我认为Yoshi已经差不多做到了,但是使用更一致,代码更可读:-

$oneMonth = new \DateInterval('P1M');
$startDate = \DateTime::createFromFormat('d H:i:s', '1 00:00:00')->sub(new \DateInterval('P4M'));
$period = new \DatePeriod($startDate, $oneMonth, 16);

foreach($period as $date){
    //$date is an instance of \DateTime. I'm just var_dumping it for illustration
    var_dump($date);
}

我认为Yoshi已经差不多做到了,但是使用更一致,代码更可读:-

$oneMonth = new \DateInterval('P1M');
$startDate = \DateTime::createFromFormat('d H:i:s', '1 00:00:00')->sub(new \DateInterval('P4M'));
$period = new \DatePeriod($startDate, $oneMonth, 16);

foreach($period as $date){
    //$date is an instance of \DateTime. I'm just var_dumping it for illustration
    var_dump($date);
}
使用是最简单、可读性更强的方法。 我会这样做:

$from = new DateTime('-4 month');
$to   = new DateTime('+12 month');
while($from < $to){
  echo $from->modify('+1 month')->format('F');
}
使用是最简单、可读性更强的方法。 我会这样做:

$from = new DateTime('-4 month');
$to   = new DateTime('+12 month');
while($from < $to){
  echo $from->modify('+1 month')->format('F');
}

从四个月前的时间戳开始,将循环扩展到16个月将是一种简单的方法,可以从四个月前的时间戳开始,将循环扩展到16个月将是一种简单的方法+1个好的方法,尽管我个人会删除一些一次性使用变量。+1个好的,虽然就我个人而言,我会删除一些一次性使用变量。如何将其修改为循环到1月到12月?@Xitish您可以调整循环变量。如果需要可读的代码,还可以创建月份列表并将月份名称存储在其中。如果您想在代码中实现它,还可以这样做:$months=array\u mapfunction$i{return date'F',mktime0,0,0,$i,1;},range1,12;。如何将其修改为循环一月到十二月?@Xitish您可以调整循环变量。如果需要可读的代码,还可以创建月份列表并将月份名称存储在其中。如果您想在代码中实现它,还可以这样做:$months=array\u mapfunction$i{return date'F',mktime0,0,0,$i,1;},range1,12;。