【问题标题】:PHP Calendar tablePHP 日历表
【发布时间】:2016-11-10 07:33:58
【问题描述】:

您好,我正在尝试制作一个显示一年中的月份和一周中的天数的日历表。

我可以为这个月(2016 年 11 月)制作一个。现在我想在这一年和未来几年循环播放它。

有人可以帮助我吗?

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

/* Set the date */
$date = strtotime(date("Y-m-d"));

$day = date('d', $date);
$month = date('m', $date);
$year = date('Y', $date);

// $nextyear = strtotime('+1 month', $date);

$firstDay = mktime(0,0,0,$month, 1, $year);
$title = strftime('%B', $firstDay);
$dayOfWeek = date('D', $firstDay);
$daysInMonth = cal_days_in_month(0, $month, $year);
/* Get the name of the week days */
$timestamp = strtotime('next Sunday');
$weekDays = array();



for ($i = 0; $i < 32; $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="32" 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) % 32 == 0): ?>
                </tr><tr>
            <?php endif; ?>
        <?php endfor; ?>
        <?php for($i = 0; ($i + $blank + $daysInMonth) % 32 != 0; $i++): ?>
            <td></td>
        <?php endfor; ?>
    </tr>
</table>

【问题讨论】:

  • 你为什么要这样做?这很痛苦!看看客户端的 JavaScript 库。它们在那里,随时可用,提供广泛的功能、选项和主题。
  • 我已经检查了其中的一些,但我需要的只是一个简单的日历表,它不像典型的日历表那样横向显示一整月
  • 啊,好的,我明白了。所以你说你有一个月,现在想循环几个月?好的,那么问题是什么?为什么不能简单地循环?无论如何,您需要一些目录来编码每月的天数。
  • 我不知道如何每月进行循环。就像如果 11 月结束,它将显示 12 月的新表格
  • 您的方法基于当前日期,这是有问题的,因为它使解决方案不灵活。相反,您必须在开始时将所有定义包装到某个以日期作为参数的函数中。然后,您可以轻松实现两个相互嵌套的循环,外部迭代数年,内部迭代数月,并调用所述函数以获得所需的定义。

标签: php calendar


【解决方案1】:

如上面的 cmets 所述,您可以使用两个相互嵌套的循环,一个用于年,一个用于月。这样,您可以根据不同的日期执行您已经给定的代码,但当前日期除外。

看看这个方法。它基本上可以满足您的需求,但是您需要对布局进行一些微调:

<?php

define('NUMBER_OF_COLUMNS', 37);    

function renderCalenderMonth($date) {
  $day = date('d', $date);
  $month = date('m', $date);
  $year = date('Y', $date);

  $firstDay = mktime(0,0,0,$month, 1, $year);
  $title = strftime('%B', $firstDay);
  $dayOfWeek = date('D', $firstDay);
  $daysInMonth = cal_days_in_month(0, $month, $year);
  /* Get the name of the week days */
  $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);

  }
}

【讨论】:

  • 嗨,我目前正在将代码迁移到 laravel 5.3,我会为此发布我的 laravel 5.3 代码。你能帮我检查一下吗?它不循环
【解决方案2】:

Laravel 5.3 版日历代码:

public function index()
{
    $users = DB::table('users')->where('roles_id', '2')->get();

    date_default_timezone_set("Asia/Hong_Kong");

    for ($iterateYear = 2017; $iterateYear < 2018; $iterateYear++) {
        for ($iterateMonth = 1; $iterateMonth <= 12; $iterateMonth++) {
            /* Set the date */
            $date = strtotime(sprintf('%s-%s-01', $iterateYear, $iterateMonth));
            return $this->renderCalenderMonth($date);
        }
    }

    return view('transaction.index', [
      'users' => $users
    ]);
}

public function renderCalenderMonth($date)
{
    // $date = strtotime(date("Y-m-d"));
    $NUMBER_OF_COLUMNS = 37;

    $day = date('d', $date);
    $month = date('m', $date);
    $year = date('Y', $date);

    $firstDay = mktime(0,0,0,$month, 1, $year);
    $title = strftime('%B', $firstDay);
    $dayOfWeek = date('D', $firstDay);
    $daysInMonth = cal_days_in_month(0, $month, $year);
    /* Get the name of the week days */
    $timestamp = strtotime('next Sunday');
    $weekDays = [];

    for ($i = 0; $i < $NUMBER_OF_COLUMNS; $i++) {
        $weekDays[] = strftime('%a', $timestamp);
        $timestamp = strtotime('+1 day', $timestamp);
    }

    $blank = date('w', strtotime("{$year}-{$month}-01"));
    $divid = $title . $year;

    return view('transaction.index', [
        'date' => $date,
        'day' => $day,
        'month' => $month,
        'year' => $year,
        'firstDay' => $firstDay,
        'title' => $title,
        'dayOfWeek' => $dayOfWeek,
        'daysInMonth' => $daysInMonth,
        'timestamp' => $timestamp,
        'weekDays' => $weekDays,
        'blank' => $blank,
        'divid' => $divid,
        'NUMBER_OF_COLUMNS' => $NUMBER_OF_COLUMNS
    ]);
}

我只知道这个。感谢您的帮助!

【讨论】:

  • 问题是你return inside 每次迭代。这意味着您在 first 迭代期间跳出该函数(返回到调用范围),因此永远不会执行后续迭代。 不要返回!如果你缓冲输出,然后将它连接到某个字符串值并在最后返回。
  • 还有一个一般提示:以这种方式交换代码非常痛苦。我建议你把你的代码放到一些修订控制系统中,比如 git 或 subversion 存储库(github 就可以了)。这样,其他人就可以使用您的代码并创建您可以查看、接受或拒绝的修改。这就是代码开发为您大大更高效的方式。
  • 我该怎么做? “如果您缓冲输出,则将其连接到某个字符串值并最终返回。” @arkascha
  • 请开始阅读文档... 对于输出缓冲从这里开始:php.net/manual/en/book.outcontrol.php 它允许您“捕获”您通过echo 或类似方式输出的内容(例如 html 标记)并将它们放入字符串变量中。然后,您可以随心所欲地使用它。例如,您可以有一个缓冲区变量并连接每次迭代捕获的月份输出。最后,您有一个变量保存所有月份和年份的完整 html 标记,然后您可以 returnecho
猜你喜欢
  • 2012-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多