将年份分成php组中的表组

时间:2017-06-23 15:00:33

标签: php date

我有这个代码..这将产生一个多年的表

$starting_year  = 2006;
$ending_year    = date("Y");

for($starting_year; $starting_year <= $ending_year; $starting_year++) {
    $years[] = ''.$starting_year.'';
}


echo "<table border='1'><tr><td>";
echo implode("<td>",$years);  

echo "</td>";
echo "</tr><tr>";

for($i=0;$i<count($years);$i++) {   
    echo "<td>mydata</td>";
}
echo "</tr></table>";

我如何拆分这个年份表,以便将它分成表格组,每个表格分组为5年。

2016,2017
2011,2012,2013,2014,2015
2006,2007,2008,2009,2010

编辑:我其实希望它看起来像这样

2016    2017            
mydata  mydata          
2011    2012    2013    2014    2015
mydata  mydata  mydata  mydata  mydata
2006    2007    2008    2009    2010
mydata  mydata  mydata  mydata  mydata

1 个答案:

答案 0 :(得分:1)

这应该可以解决问题:

$chunkSize = 5;
$starting_year  = 2006;
$ending_year    = date("Y");

for($starting_year; $starting_year <= $ending_year; $starting_year++) {
    $years[] = $starting_year;
}
echo '<table border="1">';
for($i = 0; $i < count($years);)
{
    echo '<tr>';
        echo '<td>'.implode(',', array_slice($years, $i, $chunkSize)).'</td>';
    echo '/<tr>';
    $i += 5;
}
echo '</table>';

唯一的缺点是您的<table>将被撤消。