我想在下拉框中获取当前月份和未来三个月的年份,问题是当 2012 年 11 月到来时,最后一个月将是 2013 年 1 月,如果当前月份是 2012 年 12 月,那么嵌套三个月将是
2013年1月2013年2月2013年3月
在下拉列表中,它应该看起来像
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/>';
}
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";
如果你觉得有点面向对象:
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');
}
$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'));
for($i=0;$i<4;$i++) {
echo date("M Y", strtotime($i." month", $this_month)) .'
';
}