我想获得当前月份,并添加未来三个月,并显示在我的表头
我的代码是:
$mon = (date('M'));
switch($mon) {
case 'Mar' :
echo '<th>Mar</th>';
echo '<th>Apr</th>';
echo '<th>May</th>';
break;
case 'Apr' :
echo '<th>Apr</th>';
echo '<th>May</th>';
echo '<th>Jun</th>';
break;
}
............... and so on ..............
上述开关箱将在12个月内正常工作。有没有办法在12个月里用一个开关箱而不是12个开关箱来动态地做到这一点?我疯了吗?
谢谢,Kimz
您也可以自己计算接下来的月份:
$now = time();
$currentMonth = date('n', $now);
$year = date('Y', $now);
$nextMonth = $currentMonth + 1;
$secondNextMonth = $currentMonth + 2;
echo '<th>' . date('M', $now) . '</th>';
echo '<th>' . date('M', mktime(0, 0, 0, $nextMonth, 1, $year)) . '</th>';
echo '<th>' . date('M', mktime(0, 0, 0, $secondNextMonth, 1, $year)) . '</th>';
为什么不直接打印这些值呢?
<?php
echo '<th>'.date('M').'</th>';
echo '<th>'.date('M',strtotime("+1 months")).'</th>';
echo '<th>'.date('M',strtotime("+2 months")).'</th>';
您可以使用strtotime来获取接下来的月份:
echo '<th>' . date('M') . '</th>';
echo '<th>' . date('M', strtotime('+1 months')) . '</th>';
echo '<th>' . date('M', strtotime('+2 months')) . '</th>';
下面是一段简单易懂的代码。Try it out
$month = array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
for($i=0;$i<=11;$i++)
{
if(date('M') == $month[$i])
{
$a = $i + 1;
$b = $i + 2;
if($a > 11)
{
$a = 0;
$b = 1;
}
if($b > 11)
{
$b = 0;
}
echo '<th>' . $month[$i] . '</th>';
echo '<th>' . $month[$a] . '</th>';
echo '<th>' . $month[$b] . '</th>';
}
}