繁体   English   中英

PHP日历按月

[英]Php calendar by month

我查看了100个日历代码,认为没有一个好的代码。 所以我想出了自己的代码,但是需要som帮助来了解我所缺少的内容。 现在以为我混淆了循环或其他东西,已经尝试了一段时间,但无法正确完成。 我现在得到的结果是从上到下的一行。 不要似乎工作。 somone可以帮助我了解我需要做什么吗?

<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>&nbsp;</td>";
     echo "</tr>";
}
}
?>

我已经尝试过强迫您的代码执行您想要的操作,但是恐怕存在很多逻辑问题,最好还是完全回到绘图板上。

查看您的代码,看来您想要完成的是一张表格,该表格相互向下显示当前月份的星期,而每周从星期一开始。 对于不包含日期的表单元格(例如,月的开始或结束于一周的中旬),您只想在该单元格中显示一个空格,使其为空。

此外,您想突出显示当前日期。

所以从本质上讲,今天(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>&nbsp;</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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM