首页 > 解决方案 > 日历表 PHP

问题描述

对于一个小项目,我想创建一个日历表。我想循环几个月和几天。

使用我在这一边找到的代码,它可以循环。

$timestamp = strtotime('next Sunday');
  $weekDays = array();

  for ($i = 0; $i < NUMBER_OF_COLUMNS; $i++) {
      $weekDays[] = strftime('%a', $timestamp);
      $timestamp = strtotime('+1 day', $timestamp);
  }
  $blank = date('w', strtotime("{$year}-{$month}-01"));
  ?>

  <table class='table table-bordered' style="table-layout: fixed;">
      <tr>
      <th colspan="<?php echo NUMBER_OF_COLUMNS?>" class="text-center"> <?php echo $title ?> <?php echo $year ?> </th>
      </tr>
      <tr>
      <?php foreach($weekDays as $key => $weekDay) : ?>
          <td class="text-center"><?php echo $weekDay ?></td>
      <?php endforeach ?>
      </tr>
      <tr>
      <?php for($i = 0; $i < $blank; $i++): ?>
          <td></td>
      <?php endfor; ?>
      <?php for($i = 1; $i <= $daysInMonth; $i++): ?>
          <?php if($day == $i): ?>
          <td><strong><?php echo $i ?></strong></td>
          <?php else: ?>
          <td><?php echo $i ?></td>
          <?php endif; ?>
          <?php if(($i + $blank) % NUMBER_OF_COLUMNS == 0): ?>
          </tr><tr>
          <?php endif; ?>
      <?php endfor; ?>
      <?php for($i = 0; ($i + $blank + $daysInMonth) % NUMBER_OF_COLUMNS != 0; $i++): ?>
          <td></td>
      <?php endfor; ?>
      </tr>
  </table>

  <?php
}

// ===========================

/* Set the default timezone */
date_default_timezone_set("Asia/Hong_Kong");

for ($iterateYear=2016; $iterateYear<2018; $iterateYear++) {
  for ($iterateMonth=1; $iterateMonth<=12; $iterateMonth++) {

    /* Set the date */
    $date = strtotime(sprintf('%s-%s-01', $iterateYear, $iterateMonth));
    renderCalenderMonth($date);

  }
}

但我想以这种格式创建一个表(使用大型(非高效)html代码)。

我喜欢看它:

在此处输入图像描述

标签: php

解决方案


推荐阅读