简体   繁体   English

如何获得下一年的四个月与PHP?

[英]how to get next four month with year in php?

i want to get the current month and next three months with year in a dropdown box, the proble is when November2012 comes then the last month would be January2013, if the current month is december2012 then the nest three months would be 我想在下拉框中获取带有年份的当前月份和接下来的三个月,问题是2012年11月到来的时候,最后一个月是2013年1月,如果当前月份是2012年12月,那么嵌套的三个月就是

january2013 february2013 march2013 2013年1月2013年2月2013年3月

in the drop down it should look like 在下拉菜单中应该看起来像

December2012
january2013
february2013
march2013

Try something like this: 尝试这样的事情:

$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/>';
}

If you're feeling a little object oriented: 如果您感到有点面向对象:

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');
}
echo date('F Y') . "\n";
echo date('F Y', strtotime('+1 month', time())) . "\n";
echo date('F Y', strtotime('+2 month', time())) . "\n";
echo date('F Y', strtotime('+3 month', time())) . "\n";
$t = time();
$m = date('n', $t);
$d = date('j', $t);
$y = date('Y', $t);
for ($i = 0; $i < 4; $i++)
{
    echo date('FY\n', mktime(0, 0, 0, ($m + $i - 1) % 12 + 1, $d, $y + ($m + $i > 12 ? 1 : 0)));
}

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

for($i=0;$i<4;$i++) { for($ i = 0; $ i <4; $ i ++){

echo date("MY", strtotime($i." month", $this_month)) .' echo date(“ MY”,strtotime($ i。“ month”,$ this_month))。
'; ';

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM