如何在php中获得下一个四个月和一年?

如何在php中获得下一个四个月和一年?,php,Php,我想在下拉框中获得当前月份和未来三个月的年份,问题是当2012年11月到来时,最后一个月将是2013年1月,如果当前月份是2012年12月,那么接下来的三个月将是 2013年1月 2013年2月 2013年3月2日 在下拉列表中,它应该是 December2012 january2013 february2013 march2013 试着这样做: $this_month = mktime(0, 0, 0, date('m'), 1, date('Y')); for ($i = 0; $i &l

我想在下拉框中获得当前月份和未来三个月的年份,问题是当2012年11月到来时,最后一个月将是2013年1月,如果当前月份是2012年12月,那么接下来的三个月将是

2013年1月 2013年2月 2013年3月2日

在下拉列表中,它应该是

December2012
january2013
february2013
march2013

试着这样做:

$this_month = mktime(0, 0, 0, date('m'), 1, date('Y'));
for ($i = 0; $i < 4; ++$i) {
    echo date('M Y', strtotime($i.' month', $this_month)) . '<br/>';
}
$this_month=mktime(0,0,0,date('m'),1,date('Y'));
对于($i=0;$i<4;++$i){
回显日期('my',strottime('i.'month',$this_month))。
; }
尝试以下方法:

$this_month = mktime(0, 0, 0, date('m'), 1, date('Y'));
for ($i = 0; $i < 4; ++$i) {
    echo date('M Y', strtotime($i.' month', $this_month)) . '<br/>';
}
$this_month=mktime(0,0,0,date('m'),1,date('Y'));
对于($i=0;$i<4;++$i){
回显日期('my',strottime('i.'month',$this_month))。
; }
$t=time();
$m=日期($n',$t);
$d=日期('j',$t);
$y=日期($y',$t);
对于($i=0;$i<4;$i++)
{
回显日期('FY\n',mktime(0,0,0,($m+$i-1)%12+1,$d,$y+($m+$i>12-1:0));
}
$t=time();
$m=日期($n',$t);
$d=日期('j',$t);
$y=日期($y',$t);
对于($i=0;$i<4;$i++)
{
回显日期('FY\n',mktime(0,0,0,($m+$i-1)%12+1,$d,$y+($m+$i>12-1:0));
}

如果您觉得有点面向对象:

date_default_timezone_set('Europe/Stockholm');
$now = new DateTime(date('Y-m'));
$period = new DatePeriod($now, new DateInterval('P1M'), 3);

foreach ($period as $date)
{
    print $date->format('MY');
}

如果您觉得有点面向对象:

date_default_timezone_set('Europe/Stockholm');
$now = new DateTime(date('Y-m'));
$period = new DatePeriod($now, new DateInterval('P1M'), 3);

foreach ($period as $date)
{
    print $date->format('MY');
}
$this_month=mktime(0,0,0,date('m'),1,date('Y'))

对于($i=0;$i$this_month=mktime(0,0,0,date('m'),1,date('Y'))


对于($i=0;$ii无法尝试此操作,我无法创建逻辑我无法尝试此操作,我无法创建逻辑它将。您可以使用时间戳向strotime提供第二个参数来测试此情况:echo date(“MY”,strotime(“MY”),strotime(+1个月),strotime(“2012-12-01”);这在一个月的31天将无法正常工作(如果2月是未来月份之一,则更早)。您需要将时间基于更早的日期。请将此置于顶部:
$this_month=mktime(0,0,0,date('m'),1,date('Y'))
。然后,无论何时调用strotime,都要将
$this_month
作为第二个参数传递。它会。您可以使用时间戳为strotime提供第二个参数进行测试:echo date(“MY”,strotime(“MY”),strotime”(+1个月),strotime(“2012-12-01”);这在一个月的31天将无法正常工作(如果2月是未来月份之一,则更早)。您需要将时间基于更早的日期。请将此置于顶部:
$this_month=mktime(0,0,0,date('m'),1,date('Y'));
。然后,无论何时调用strotime,都要将
$this\u month
作为第二个参数传递。如果一个月的最后一天是31天,或者如果二月是未来的月份之一,则这将不起作用。请尝试添加
$this\u month=mktime(0,0,date('m'),1,date('Y');
然后替换对
time()的所有调用
使用
$this\u month
。如果二月是未来的月份之一,则在31天内或更早的月份的最后一天不起作用。请尝试添加
$this\u month=mktime(0,0,0,date('m'),1,date('Y');
然后将对
time()
的所有调用替换为
$this\u month