在接下来的12个月里得到今天


get current day in next 12 months

有可能在未来12个月内得到当前的一天吗?

看一下这些场景:

Today is 2014-05-09
next month will be 2014-06-09
and so on and so fourth
what if today is 2014-01-31
next month is 2014-02-28
Other examples
what if today is 2014-05-31
next month will be 2014-06-30

谢谢

试试这个。即使日期是一个月的最后一天也可以正常工作

$date = "2013-03-31";
$d1 = explode("-",$date);
$i = 0;
while ($i < 12)
{
$j = $d1[2];
if(cal_days_in_month(CAL_GREGORIAN, $d1[1], $d1[0]) < $d1[2])
$j = cal_days_in_month(CAL_GREGORIAN, $d1[1], $d1[0]) ;
echo $d1[0]."-".$d1[1]."-".$j."<br>";
$d1[1]++;
if($d1[1] == 13)
{
$d1[0]++;
$d1[1] = 1;
}
$i++;
if($d1[1] < 10)
$d1[1]="0".$d1[1];
}

可以给给定的日期添加一个DateInterval。但是你的例子没有统一的逻辑。

例如:

$date = new DateTime( '2014-01-31' );
$aMonthLater = clone $date;
$aMonthLater->add( new DateInterval( 'P1M' ) );

var_dump(
    $date->format( DateTime::ISO8601 ),
    $aMonthLater->format( DateTime::ISO8601 )
);

这会导致如下输出:

2014-01-31T00:00:00+01:00
2014-03-03T00:00:00+01:00

因此,增加一个月实际上会增加一个完整的月-您的示例(从2014-01-31跳到2014-02-28)不会增加一个完整的月。

看到DateTime: add ()