未来12个月,过去12个月 [英] GET 12 months in future and 12 months in past

查看:109
本文介绍了未来12个月,过去12个月的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何循环过去12个月以及未来的12个月。

How can i loop through the past 12 months and also the future 12 months.

我最简单的方式是在过去24个月内循环,开始一个一年前例如2015年1月

The easiest way i imainge is just looping through the past 24 months by starting a year ahead e.g. Jan 2015

我目前的代码返回过去12个月

My current code returns the past 12 months

for ($i = 1; $i <= 12; $i++) {
$my = date("F Y", strtotime( date( 'Y-m-01' )." -$i months"));
$ymd = date("Y-m-d", strtotime( date( 'Y-m-01' )." -$i months"));


推荐答案

这可以通过 DateTime extension:

This can simply be accomplished via DateTime extension:

$months = 12; # number of months before and after current month
$dt = new DateTime('first day of this month'); # select first day in current month
$dt->modify("-$months month");
for ($i = 0; $i <= $months * 2; $i++) {
    echo $dt->format('M Y'), "\n";
    $dt->modify('+1 month');
}

演示

这篇关于未来12个月,过去12个月的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆