我查看了100个日历代码,但没有一个好的代码。所以我想出了自己的代码,但是需要som帮助来了解我所缺少的内容。现在以为我混淆了循环或其他东西,已经尝试了一段时间,但无法正确完成。我现在得到的结果是从上到下的一行。不要似乎工作。有人可以帮我了解我需要做什么吗?
<table border="1">
<?php
//counter array
$counter = 0;
//start days array
$list=array();
//current day
$curDay = date("28", $today);
// mktime(0, 0, 0, date("m"), date("d") , date("Y"));
$today = mktime(0, 0, 0, date("m"), '28', date("Y"));
// curent month
$curentmonth = '09';
// curent year
$year = 2018;
// curent month
$month = date('m');
//loop number of days in month and list them in the array
for($d=1; $d<=31; $d++)
{
$datetime=mktime(12, 0, 0, $curentmonth, $d, $year);
if (date('m', $datetime)==$curentmonth)
$list[]=date('d', $datetime);
//try to get the right 7 weeks in a month start monday
for ($day = 1; $day <=7; $day++) {
$datetime = strtotime($year.'W'.$month.$day);
echo "<tr>";
}
// make this day red
if ($curentmonth === $month && $list[$counter] === $curDay) {
echo "<td bgcolor='ff0000'>$list[$counter]</td>";
$counter++;
// all other days in the month
} elseif ($month === $curentmonth) {
echo "<td>$list[$counter]</td>";
$counter++;
// other month
} else {
echo "<td> </td>";
echo "</tr>";
}
}
?>
答案 0 :(得分:0)
我已经尝试过强迫您的代码执行您想要的操作,但是恐怕存在太多逻辑问题,最好完全回到绘图板上。
看看您的代码,看来您想要完成的是一张表格,该表格彼此之间依次显示当前月份的星期,而每周从星期一开始。对于不包含日期的表格单元格(例如,月份的开始或结束时间是一周的中旬),您只希望在该单元格中显示一个空格,使其为空。
此外,您还希望突出显示当前日期。
从本质上讲,今天(2018年9月30日)的预期最终结果是这样的:
+----+----+----+----+----+----+------+
| | | | | | 1 | 2 |
+----+----+----+----+----+----+------+
| 3 | 4 | 5 | 6 | 7 | 8 | 9 |
+----+----+----+----+----+----+------+
| 10 | 11 | 12 | 13 | 14 | 15 | 16 |
+----+----+----+----+----+----+------+
| 17 | 18 | 19 | 20 | 21 | 22 | 23 |
+----+----+----+----+----+----+------+
| 24 | 25 | 26 | 27 | 28 | 29 | *30* |
+----+----+----+----+----+----+------+
有很多方法可以实现这一目标。这是一种通过简单的日期操作来实现的方法,因此不需要数组和多个循环:
// the month to render and day to highlight
$today = new DateTime();
// we'll need these to check if we're at the day we need to highlight
// or if we're rendering a date that's outside $today's month
$currentDay = $today->format('j');
$currentMonth = $today->format('n');
$daysInCurrentMonth = $today->format('t');
// figure out what Monday needs to kick off our table
$date = (clone $today)->modify('first day of this month');
if ($date->format('w') != 1) {
$date->modify('previous monday');
}
$shouldStopRendering = false;
echo '<table border="1">';
while (!$shouldStopRendering) {
$weekDay = $date->format('w');
$month = $date->format('n');
$day = $date->format('j');
// start a new table row every time we hit a Monday, note that
// since we forced $date to be a Monday above, our table should
// now always start with a <tr>
if ($weekDay == 1) {
echo '<tr>';
}
if ($month != $currentMonth) {
// we're either at the beginning or end of our table
echo '<td> </td>';
} else if ($day == $currentDay) {
// highlight the current date
echo '<td bgcolor="#ff0000">' . $day . '</td>';
} else {
echo '<td>' . $day . '</td>';
}
if ($weekDay == 0) {
// every time we hit a Sunday, close the current table row
echo '</tr>';
// on a Sunday, if we've gone outside the current month **or**
// this Sunday happens to be the last day we need to render,
// stop looping so we can close the table
if ($month != $currentMonth || $day == $daysInCurrentMonth) {
$shouldStopRendering = true;
}
}
// move on to the next day we need to display
$date->modify('+1 day');
}
echo '</table>';
注意这一行:
$date = (clone $today)->modify('first day of this month');
我克隆$today
而不是仅仅创建一个新的DateTime
实例的原因是,您可能希望更改$today
以呈现不同的月份。或者,也许您要花几个月的时间来渲染一整年的日历。我不知道是哪种情况,所以我以$date
刚发生的情况为基础$today
。
不仅仅是这样做:
$date = $today->modify('first day of this month');
这将起作用,但是它还会修改$today
,如果您想在呈现表格后重新使用当前日期,则可能不是您想要的。
如果您使用的是旧版本的PHP,则可能不允许您在一行中单独执行(clone $today)->modify
。如果是这样,只需将其分成两行即可:
$date = clone $today;
$date->modify('first day of this month');
以下是此代码的演示示例(为生成的HTML的可读性添加了一些空格):https://3v4l.org/Z89i8