【发布时间】: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 月的新表格
-
您的方法基于当前日期,这是有问题的,因为它使解决方案不灵活。相反,您必须在开始时将所有定义包装到某个以日期作为参数的函数中。然后,您可以轻松实现两个相互嵌套的循环,外部迭代数年,内部迭代数月,并调用所述函数以获得所需的定义。