我试着做一个日期到日期的年月表,但是我不能让它像我想要的那样工作。
注意:日期将在我的应用程序中动态更改。
请看下面的图片。
这是我的代码:
<?php
$start = (new DateTime('2020-08-02'))->modify('first day of this month');
$end = (new DateTime('2022-05-06'))->modify('first day of next month');
$interval = DateInterval::createFromDateString('1 month');
$period = new DatePeriod($start, $interval, $end);
?>
<table border="1">
<tr>
<?php
foreach ($period as $dt) {
echo "<td>" . $dt->format("Y") . "</td>";
}
?>
</tr>
<tr>
<?php
foreach ($period as $dt) {
echo "<td>" . $dt->format("m") . "</td>";
}
?>
</tr>
</table>
也许还有另一个简单的办法。 但这个对我来说很好。 为了消除重复值,我创建了一个名为$AR的新数组,并将所有年份值作为键存储在其中。 这将有助于消除所有重复值。
<?php
$start = (new DateTime('2020-08-02'))->modify('first day of this month');
$end = (new DateTime('2022-05-06'))->modify('first day of next month');
$interval = DateInterval::createFromDateString('1 month');
$period = new DatePeriod($start, $interval, $end);
?>
<table border="1">
<tr>
<?php
$ar = array();
$th_span = array(5,12,5);
$i = 0;
foreach ($period as $dt) {
$ar += array($dt->format("Y") => 0);
}
foreach($ar as $k => $v) {
echo "<th colspan=".$th_span[$i].">" . $k . "</th>";
$i++;
}
?>
</tr>
<tr>
<?php
foreach ($period as $dt) {
echo "<td>" . $dt->format("F") . "</td>";
}
?>
</tr>
</table>