codeigniter 如何使用foreach循环创建一个包含当前月份日期的html表列

k97glaaz  于 2023-04-27  发布在  其他
关注(0)|答案(2)|浏览(112)

如何在php中使用foreach循环创建一个包含当前月份日期列的表?

yyyllmsg

yyyllmsg1#

<table>
  <thead>
    <tr>
      <th>Date</th>
    </tr>
  </thead>
  <tbody>
    <?php
      $m = date('m'); // current month
      $y = date('Y'); // current year

      foreach(range(1, date('t')) as $d) { 
    /* loop through each day in the current month */
        echo '<tr><td class="date">' . $y . '-' . $m . '-' . $d . '</td></tr>';
      }
    ?>
  </tbody>
</table>
jhdbpxl9

jhdbpxl92#

<?php
// Set the timezone to your preferred timezone
date_default_timezone_set('UTC');

// Get the current month and year
$month = date('m');
$year = date('Y');

// Get the number of days in the current month
$days_in_month = cal_days_in_month(CAL_GREGORIAN, $month, $year);

// Create an array of dates in the current month
$dates = array();
for ($day = 1; $day <= $days_in_month; $day++) {
$date = new DateTime("$year-$month-$day");
$dates[] = $date->format('Y-m-d');
}

// Output the dates in an HTML table column
echo "<table>";
echo "<tr>";
echo "<th>Dates</th>";
echo "</tr>";
foreach ($dates as $date) {
echo "<tr>";
echo "<td>$date</td>";
echo "</tr>";
}
echo "</table>";
?>

相关问题