回显所有月份与日期日历

发布于 2024-10-29 02:23:51 字数 1497 浏览 7 评论 0 原文

我使用下面的代码来回显当前月份。我怎样才能增强它,以便显示所有月份的名称、日期和日期..

代码:

 <?php
$today    = getdate();
$firstDay = getdate(mktime(0,0,0,$today['mon'],1,$today['year']));
$lastDay  = getdate(mktime(0,0,0,$today['mon']+1,0,$today['year']));


?>

<?php

echo '<table>';
echo '  <tr><th colspan="7">'.$today['month']." - ".$today['year']."</th></tr>";
echo '<tr class="days">';
echo '  <td>Mo</td><td>Tu</td><td>We</td><td>Th</td>';
echo '  <td>Fr</td><td>Sa</td><td>Su</td></tr>';
?> 

<?php
echo '<tr>';
for($i=1;$i<$firstDay['wday'];$i++){
    echo '<td>&nbsp;</td>';
}
$actday = 0;
for($i=$firstDay['wday'];$i<=7;$i++){
    $actday++;
    echo "<td>$actday</td>";
}
echo '</tr>';
?> 

<?php
$fullWeeks = floor(($lastDay['mday']-$actday)/7);

for ($i=0;$i<$fullWeeks;$i++){
    echo '<tr>';
    for ($j=0;$j<7;$j++){
        $actday++;
        echo "<td>$actday</td>";
    }
    echo '</tr>';
    }
    ?> 

    <?php
    if ($actday < $lastDay['mday']){
    echo '<tr>';

    for ($i=0; $i<7;$i++){
        $actday++;
        if ($actday <= $lastDay['mday']){
            echo "<td>$actday</td>";
        }
        else {
            echo '<td>&nbsp;</td>';
        }
    }

    echo '</tr>';
}
?> 

I am using the code below to echo the current month. How can i enhance it so that is shows all the months with the names and days and dates..

Code:

 <?php
$today    = getdate();
$firstDay = getdate(mktime(0,0,0,$today['mon'],1,$today['year']));
$lastDay  = getdate(mktime(0,0,0,$today['mon']+1,0,$today['year']));


?>

<?php

echo '<table>';
echo '  <tr><th colspan="7">'.$today['month']." - ".$today['year']."</th></tr>";
echo '<tr class="days">';
echo '  <td>Mo</td><td>Tu</td><td>We</td><td>Th</td>';
echo '  <td>Fr</td><td>Sa</td><td>Su</td></tr>';
?> 

<?php
echo '<tr>';
for($i=1;$i<$firstDay['wday'];$i++){
    echo '<td> </td>';
}
$actday = 0;
for($i=$firstDay['wday'];$i<=7;$i++){
    $actday++;
    echo "<td>$actday</td>";
}
echo '</tr>';
?> 

<?php
$fullWeeks = floor(($lastDay['mday']-$actday)/7);

for ($i=0;$i<$fullWeeks;$i++){
    echo '<tr>';
    for ($j=0;$j<7;$j++){
        $actday++;
        echo "<td>$actday</td>";
    }
    echo '</tr>';
    }
    ?> 

    <?php
    if ($actday < $lastDay['mday']){
    echo '<tr>';

    for ($i=0; $i<7;$i++){
        $actday++;
        if ($actday <= $lastDay['mday']){
            echo "<td>$actday</td>";
        }
        else {
            echo '<td> </td>';
        }
    }

    echo '</tr>';
}
?> 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

情深已缘浅 2024-11-05 02:23:51

试试这个:

function getDates($year)
{
    $dates = array();

    date("L", mktime(0,0,0, 7,7, $year)) ? $days = 366 : $days = 365;
    for($i = 1; $i <= $days; $i++){
        $month = date('m', mktime(0,0,0,1,$i,$year));
        $wk = date('W', mktime(0,0,0,1,$i,$year));
        $wkDay = date('D', mktime(0,0,0,1,$i,$year));
        $day = date('d', mktime(0,0,0,1,$i,$year));

        $dates[$month][$wk][$wkDay] = $day;
    } 

    return $dates;   
}

它将返回传递给函数的年份的月份->周->日->工作日的数组。希望可以很容易地遍历数组来打印所有内容。我确信您可以对此进行很多调整,但这只是一个开始。

我也会尝试避免使用 echo 打印 html,例如,而不是;

echo '<tr>';
for($i=1;$i<$firstDay['wday'];$i++){
    echo '<td> </td>';
}

做;

<tr>;
<?php for($i=1;$i<$firstDay['wday'];$i++){ ?>
    <td><?php echo $var; ?></td>
<?php } ?>

我认为这使得代码更具可读性。

编辑:只是想我还应该包括一个用例的示例,如下所示:

<?php $dates = getDates(2011); 

$weekdays = array('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'); ?>
<?php foreach($dates as $month => $weeks) { ?>
<table>
    <tr>
        <th><?php echo implode('</th><th>', $weekdays); ?></th>
    </tr>
    <?php foreach($weeks as $week => $days){ ?>
    <tr>
        <?php foreach($weekdays as $day){ ?>
        <td>
            <?php echo isset($days[$day]) ? $days[$day] : ' '; ?>
        </td>               
        <?php } ?>
    </tr>
    <?php } ?>
</table>
<?php } ?>

这将为您提供输出:

>

Try this:

function getDates($year)
{
    $dates = array();

    date("L", mktime(0,0,0, 7,7, $year)) ? $days = 366 : $days = 365;
    for($i = 1; $i <= $days; $i++){
        $month = date('m', mktime(0,0,0,1,$i,$year));
        $wk = date('W', mktime(0,0,0,1,$i,$year));
        $wkDay = date('D', mktime(0,0,0,1,$i,$year));
        $day = date('d', mktime(0,0,0,1,$i,$year));

        $dates[$month][$wk][$wkDay] = $day;
    } 

    return $dates;   
}

it will return an array of months->weeks->day->weekday of the year you pass to the function. Hopefully it should be easy to traverse through the array to print everything out. Am sure there are a lot of tweaks you can make to that but its a start.

I would also try and stay away from printing out html using echo, for example instead of;

echo '<tr>';
for($i=1;$i<$firstDay['wday'];$i++){
    echo '<td> </td>';
}

do;

<tr>;
<?php for($i=1;$i<$firstDay['wday'];$i++){ ?>
    <td><?php echo $var; ?></td>
<?php } ?>

It kind of makes the code more readable I think.

EDIT: Just thought I should include an example of a use case as well, as below:

<?php $dates = getDates(2011); 

$weekdays = array('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'); ?>
<?php foreach($dates as $month => $weeks) { ?>
<table>
    <tr>
        <th><?php echo implode('</th><th>', $weekdays); ?></th>
    </tr>
    <?php foreach($weeks as $week => $days){ ?>
    <tr>
        <?php foreach($weekdays as $day){ ?>
        <td>
            <?php echo isset($days[$day]) ? $days[$day] : ' '; ?>
        </td>               
        <?php } ?>
    </tr>
    <?php } ?>
</table>
<?php } ?>

Which gives you the output:

enter image description here

司马昭之心 2024-11-05 02:23:51

您可以使用此函数将全年转换为数组

  function year2array($year) {
    $res = $year >= 1970;
    if ($res) {
      // this line gets and sets same timezone, don't ask why :)
      date_default_timezone_set(date_default_timezone_get());

      $dt = strtotime("-1 day", strtotime("$year-01-01 00:00:00"));
      $res = array();
      $week = array_fill(1, 7, false);
      $last_month = 1;
      $w = 1;
      do {
        $dt = strtotime('+1 day', $dt);
        $dta = getdate($dt);
        $wday = $dta['wday'] == 0 ? 7 : $dta['wday'];
        if (($dta['mon'] != $last_month) || ($wday == 1)) {
          if ($week[1] || $week[7]) $res[$last_month][] = $week;
          $week = array_fill(1, 7, false);
          $last_month = $dta['mon'];
          }
        $week[$wday] = $dta['mday'];
        }
      while ($dta['year'] == $year);
      }
    return $res;
    }

调用它,

print_r(year2array(2011));

您将在源代码中看到这一点(月->周->天):

Array
(
    [1] => Array
        (
            [0] => Array
                (
                    [1] => 
                    [2] => 
                    [3] => 
                    [4] => 
                    [5] => 
                    [6] => 1
                    [7] => 2
                )

            [1] => Array
                (
                    [1] => 3
                    [2] => 4
                    [3] => 5
                    [4] => 6
                    [5] => 7
                    [6] => 8
                    [7] => 9
                )

            [2] => Array
                (
                    [1] => 10
                    [2] => 11
                    [3] => 12
                    [4] => 13
                    [5] => 14
                    [6] => 15
                    [7] => 16
                )

            [3] => Array
                (
                    [1] => 17
                    [2] => 18
                    [3] => 19
                    [4] => 20
                    [5] => 21
                    [6] => 22
                    [7] => 23
                )

            [4] => Array
                (
                    [1] => 24
                    [2] => 25
                    [3] => 26
                    [4] => 27
                    [5] => 28
                    [6] => 29
                    [7] => 30
                )

            [5] => Array
                (
                    [1] => 31
                    [2] => 
                    [3] => 
                    [4] => 
                    [5] => 
                    [6] => 
                    [7] => 
                )

        )

    [2] => Array
        (
            [0] => Array
                (
                    [1] => 
                    [2] => 1
                    [3] => 2
                    [4] => 3
                    [5] => 4
                    [6] => 5
                    [7] => 6
                )

            [1] => Array
                (
                    [1] => 7
                    [2] => 8
                    [3] => 9
                    [4] => 10
                    [5] => 11
                    [6] => 12
                    [7] => 13
                )

            [2] => Array
                (
                    [1] => 14
                    [2] => 15
                    [3] => 16
                    [4] => 17
                    [5] => 18
                    [6] => 19
                    [7] => 20
                )

            [3] => Array
                (
                    [1] => 21
                    [2] => 22
                    [3] => 23
                    [4] => 24
                    [5] => 25
                    [6] => 26
                    [7] => 27
                )

            [4] => Array
                (
                    [1] => 28
                    [2] => 
                    [3] => 
                    [4] => 
                    [5] => 
                    [6] => 
                    [7] => 
                )

        )

    [3] => Array
        (
            [0] => Array
                (
                    [1] => 
                    [2] => 1
                    [3] => 2
                    [4] => 3
                    [5] => 4
                    [6] => 5
                    [7] => 6
                )

            [1] => Array
                (
                    [1] => 7
                    [2] => 8
                    [3] => 9
                    [4] => 10
                    [5] => 11
                    [6] => 12
                    [7] => 13
                )

            [2] => Array
                (
                    [1] => 14
                    [2] => 15
                    [3] => 16
                    [4] => 17
                    [5] => 18
                    [6] => 19
                    [7] => 20
                )

            [3] => Array
                (
                    [1] => 21
                    [2] => 22
                    [3] => 23
                    [4] => 24
                    [5] => 25
                    [6] => 26
                    [7] => 27
                )

            [4] => Array
                (
                    [1] => 28
                    [2] => 29
                    [3] => 30
                    [4] => 31
                    [5] => 
                    [6] => 
                    [7] => 
                )

        )

    [4] => Array
        (
            [0] => Array
                (
                    [1] => 
                    [2] => 
                    [3] => 
                    [4] => 
                    [5] => 1
                    [6] => 2
                    [7] => 3
                )

            [1] => Array
                (
                    [1] => 4
                    [2] => 5
                    [3] => 6
                    [4] => 7
                    [5] => 8
                    [6] => 9
                    [7] => 10
                )

            [2] => Array
                (
                    [1] => 11
                    [2] => 12
                    [3] => 13
                    [4] => 14
                    [5] => 15
                    [6] => 16
                    [7] => 17
                )

            [3] => Array
                (
                    [1] => 18
                    [2] => 19
                    [3] => 20
                    [4] => 21
                    [5] => 22
                    [6] => 23
                    [7] => 24
                )

            [4] => Array
                (
                    [1] => 25
                    [2] => 26
                    [3] => 27
                    [4] => 28
                    [5] => 29
                    [6] => 30
                    [7] => 
                )

        )

    [5] => Array
        (
            [0] => Array
                (
                    [1] => 
                    [2] => 
                    [3] => 
                    [4] => 
                    [5] => 
                    [6] => 
                    [7] => 1
                )

            [1] => Array
                (
                    [1] => 2
                    [2] => 3
                    [3] => 4
                    [4] => 5
                    [5] => 6
                    [6] => 7
                    [7] => 8
                )

            [2] => Array
                (
                    [1] => 9
                    [2] => 10
                    [3] => 11
                    [4] => 12
                    [5] => 13
                    [6] => 14
                    [7] => 15
                )

            [3] => Array
                (
                    [1] => 16
                    [2] => 17
                    [3] => 18
                    [4] => 19
                    [5] => 20
                    [6] => 21
                    [7] => 22
                )

            [4] => Array
                (
                    [1] => 23
                    [2] => 24
                    [3] => 25
                    [4] => 26
                    [5] => 27
                    [6] => 28
                    [7] => 29
                )

            [5] => Array
                (
                    [1] => 30
                    [2] => 31
                    [3] => 
                    [4] => 
                    [5] => 
                    [6] => 
                    [7] => 
                )

        )

    [6] => Array
        (
            [0] => Array
                (
                    [1] => 
                    [2] => 
                    [3] => 1
                    [4] => 2
                    [5] => 3
                    [6] => 4
                    [7] => 5
                )

            [1] => Array
                (
                    [1] => 6
                    [2] => 7
                    [3] => 8
                    [4] => 9
                    [5] => 10
                    [6] => 11
                    [7] => 12
                )

            [2] => Array
                (
                    [1] => 13
                    [2] => 14
                    [3] => 15
                    [4] => 16
                    [5] => 17
                    [6] => 18
                    [7] => 19
                )

            [3] => Array
                (
                    [1] => 20
                    [2] => 21
                    [3] => 22
                    [4] => 23
                    [5] => 24
                    [6] => 25
                    [7] => 26
                )

            [4] => Array
                (
                    [1] => 27
                    [2] => 28
                    [3] => 29
                    [4] => 30
                    [5] => 
                    [6] => 
                    [7] => 
                )

        )

    [7] => Array
        (
            [0] => Array
                (
                    [1] => 
                    [2] => 
                    [3] => 
                    [4] => 
                    [5] => 1
                    [6] => 2
                    [7] => 3
                )

            [1] => Array
                (
                    [1] => 4
                    [2] => 5
                    [3] => 6
                    [4] => 7
                    [5] => 8
                    [6] => 9
                    [7] => 10
                )

            [2] => Array
                (
                    [1] => 11
                    [2] => 12
                    [3] => 13
                    [4] => 14
                    [5] => 15
                    [6] => 16
                    [7] => 17
                )

            [3] => Array
                (
                    [1] => 18
                    [2] => 19
                    [3] => 20
                    [4] => 21
                    [5] => 22
                    [6] => 23
                    [7] => 24
                )

            [4] => Array
                (
                    [1] => 25
                    [2] => 26
                    [3] => 27
                    [4] => 28
                    [5] => 29
                    [6] => 30
                    [7] => 31
                )

        )

    [8] => Array
        (
            [0] => Array
                (
                    [1] => 1
                    [2] => 2
                    [3] => 3
                    [4] => 4
                    [5] => 5
                    [6] => 6
                    [7] => 7
                )

            [1] => Array
                (
                    [1] => 8
                    [2] => 9
                    [3] => 10
                    [4] => 11
                    [5] => 12
                    [6] => 13
                    [7] => 14
                )

            [2] => Array
                (
                    [1] => 15
                    [2] => 16
                    [3] => 17
                    [4] => 18
                    [5] => 19
                    [6] => 20
                    [7] => 21
                )

            [3] => Array
                (
                    [1] => 22
                    [2] => 23
                    [3] => 24
                    [4] => 25
                    [5] => 26
                    [6] => 27
                    [7] => 28
                )

            [4] => Array
                (
                    [1] => 29
                    [2] => 30
                    [3] => 31
                    [4] => 
                    [5] => 
                    [6] => 
                    [7] => 
                )

        )

    [9] => Array
        (
            [0] => Array
                (
                    [1] => 
                    [2] => 
                    [3] => 
                    [4] => 1
                    [5] => 2
                    [6] => 3
                    [7] => 4
                )

            [1] => Array
                (
                    [1] => 5
                    [2] => 6
                    [3] => 7
                    [4] => 8
                    [5] => 9
                    [6] => 10
                    [7] => 11
                )

            [2] => Array
                (
                    [1] => 12
                    [2] => 13
                    [3] => 14
                    [4] => 15
                    [5] => 16
                    [6] => 17
                    [7] => 18
                )

            [3] => Array
                (
                    [1] => 19
                    [2] => 20
                    [3] => 21
                    [4] => 22
                    [5] => 23
                    [6] => 24
                    [7] => 25
                )

            [4] => Array
                (
                    [1] => 26
                    [2] => 27
                    [3] => 28
                    [4] => 29
                    [5] => 30
                    [6] => 
                    [7] => 
                )

        )

    [10] => Array
        (
            [0] => Array
                (
                    [1] => 
                    [2] => 
                    [3] => 
                    [4] => 
                    [5] => 
                    [6] => 1
                    [7] => 2
                )

            [1] => Array
                (
                    [1] => 3
                    [2] => 4
                    [3] => 5
                    [4] => 6
                    [5] => 7
                    [6] => 8
                    [7] => 9
                )

            [2] => Array
                (
                    [1] => 10
                    [2] => 11
                    [3] => 12
                    [4] => 13
                    [5] => 14
                    [6] => 15
                    [7] => 16
                )

            [3] => Array
                (
                    [1] => 17
                    [2] => 18
                    [3] => 19
                    [4] => 20
                    [5] => 21
                    [6] => 22
                    [7] => 23
                )

            [4] => Array
                (
                    [1] => 24
                    [2] => 25
                    [3] => 26
                    [4] => 27
                    [5] => 28
                    [6] => 29
                    [7] => 30
                )

            [5] => Array
                (
                    [1] => 31
                    [2] => 
                    [3] => 
                    [4] => 
                    [5] => 
                    [6] => 
                    [7] => 
                )

        )

    [11] => Array
        (
            [0] => Array
                (
                    [1] => 
                    [2] => 1
                    [3] => 2
                    [4] => 3
                    [5] => 4
                    [6] => 5
                    [7] => 6
                )

            [1] => Array
                (
                    [1] => 7
                    [2] => 8
                    [3] => 9
                    [4] => 10
                    [5] => 11
                    [6] => 12
                    [7] => 13
                )

            [2] => Array
                (
                    [1] => 14
                    [2] => 15
                    [3] => 16
                    [4] => 17
                    [5] => 18
                    [6] => 19
                    [7] => 20
                )

            [3] => Array
                (
                    [1] => 21
                    [2] => 22
                    [3] => 23
                    [4] => 24
                    [5] => 25
                    [6] => 26
                    [7] => 27
                )

            [4] => Array
                (
                    [1] => 28
                    [2] => 29
                    [3] => 30
                    [4] => 
                    [5] => 
                    [6] => 
                    [7] => 
                )

        )

    [12] => Array
        (
            [0] => Array
                (
                    [1] => 
                    [2] => 
                    [3] => 
                    [4] => 1
                    [5] => 2
                    [6] => 3
                    [7] => 4
                )

            [1] => Array
                (
                    [1] => 5
                    [2] => 6
                    [3] => 7
                    [4] => 8
                    [5] => 9
                    [6] => 10
                    [7] => 11
                )

            [2] => Array
                (
                    [1] => 12
                    [2] => 13
                    [3] => 14
                    [4] => 15
                    [5] => 16
                    [6] => 17
                    [7] => 18
                )

            [3] => Array
                (
                    [1] => 19
                    [2] => 20
                    [3] => 21
                    [4] => 22
                    [5] => 23
                    [6] => 24
                    [7] => 25
                )

            [4] => Array
                (
                    [1] => 26
                    [2] => 27
                    [3] => 28
                    [4] => 29
                    [5] => 30
                    [6] => 31
                    [7] => 
                )

        )

)

因此,现在可以轻松地使用类似的方法为您需要的每个月创建月份表使用

  function month2table($month, $calendar_array) {
    $ca = 'align="center"';
    $res = "<table cellpadding=\"2\" cellspacing=\"1\" style=\"border:solid 1px #000000;font-family:tahoma;font-size:12px;background-color:#ababab\"><tr><td $ca>Mo</td><td $ca>Tu</td><td $ca>We</td><td $ca>Th</td><td $ca>Fr</td><td $ca>Sa</td><td $ca>Su</td></tr>";
    foreach ($calendar_array[$month] as $month=>$week) {
      $res .= '<tr>';
      foreach ($week as $day) {
        $res .= '<td align="right" width="20" bgcolor="#ffffff">' . ($day ? $day : ' ') . '</td>';
        }
      $res .= '</tr>';
      }
    $res .= '</table>';
    return $res;
    }

这些函数,如

  $calarr = year2array(2011);
  echo month2table(1, $calarr); // January
  echo month2table(2, $calarr); // February
  ...
  echo month2table(12, $calarr); // December

..或将月份放入 for 循环中。

因此...例如,对于 2011 年 1 月,您会在浏览器中看到此

在此处输入图像描述

希望这会有所帮助。

You can use this function to convert entire year into array

  function year2array($year) {
    $res = $year >= 1970;
    if ($res) {
      // this line gets and sets same timezone, don't ask why :)
      date_default_timezone_set(date_default_timezone_get());

      $dt = strtotime("-1 day", strtotime("$year-01-01 00:00:00"));
      $res = array();
      $week = array_fill(1, 7, false);
      $last_month = 1;
      $w = 1;
      do {
        $dt = strtotime('+1 day', $dt);
        $dta = getdate($dt);
        $wday = $dta['wday'] == 0 ? 7 : $dta['wday'];
        if (($dta['mon'] != $last_month) || ($wday == 1)) {
          if ($week[1] || $week[7]) $res[$last_month][] = $week;
          $week = array_fill(1, 7, false);
          $last_month = $dta['mon'];
          }
        $week[$wday] = $dta['mday'];
        }
      while ($dta['year'] == $year);
      }
    return $res;
    }

Call it like

print_r(year2array(2011));

You'll see this in source (months->weeks->days):

Array
(
    [1] => Array
        (
            [0] => Array
                (
                    [1] => 
                    [2] => 
                    [3] => 
                    [4] => 
                    [5] => 
                    [6] => 1
                    [7] => 2
                )

            [1] => Array
                (
                    [1] => 3
                    [2] => 4
                    [3] => 5
                    [4] => 6
                    [5] => 7
                    [6] => 8
                    [7] => 9
                )

            [2] => Array
                (
                    [1] => 10
                    [2] => 11
                    [3] => 12
                    [4] => 13
                    [5] => 14
                    [6] => 15
                    [7] => 16
                )

            [3] => Array
                (
                    [1] => 17
                    [2] => 18
                    [3] => 19
                    [4] => 20
                    [5] => 21
                    [6] => 22
                    [7] => 23
                )

            [4] => Array
                (
                    [1] => 24
                    [2] => 25
                    [3] => 26
                    [4] => 27
                    [5] => 28
                    [6] => 29
                    [7] => 30
                )

            [5] => Array
                (
                    [1] => 31
                    [2] => 
                    [3] => 
                    [4] => 
                    [5] => 
                    [6] => 
                    [7] => 
                )

        )

    [2] => Array
        (
            [0] => Array
                (
                    [1] => 
                    [2] => 1
                    [3] => 2
                    [4] => 3
                    [5] => 4
                    [6] => 5
                    [7] => 6
                )

            [1] => Array
                (
                    [1] => 7
                    [2] => 8
                    [3] => 9
                    [4] => 10
                    [5] => 11
                    [6] => 12
                    [7] => 13
                )

            [2] => Array
                (
                    [1] => 14
                    [2] => 15
                    [3] => 16
                    [4] => 17
                    [5] => 18
                    [6] => 19
                    [7] => 20
                )

            [3] => Array
                (
                    [1] => 21
                    [2] => 22
                    [3] => 23
                    [4] => 24
                    [5] => 25
                    [6] => 26
                    [7] => 27
                )

            [4] => Array
                (
                    [1] => 28
                    [2] => 
                    [3] => 
                    [4] => 
                    [5] => 
                    [6] => 
                    [7] => 
                )

        )

    [3] => Array
        (
            [0] => Array
                (
                    [1] => 
                    [2] => 1
                    [3] => 2
                    [4] => 3
                    [5] => 4
                    [6] => 5
                    [7] => 6
                )

            [1] => Array
                (
                    [1] => 7
                    [2] => 8
                    [3] => 9
                    [4] => 10
                    [5] => 11
                    [6] => 12
                    [7] => 13
                )

            [2] => Array
                (
                    [1] => 14
                    [2] => 15
                    [3] => 16
                    [4] => 17
                    [5] => 18
                    [6] => 19
                    [7] => 20
                )

            [3] => Array
                (
                    [1] => 21
                    [2] => 22
                    [3] => 23
                    [4] => 24
                    [5] => 25
                    [6] => 26
                    [7] => 27
                )

            [4] => Array
                (
                    [1] => 28
                    [2] => 29
                    [3] => 30
                    [4] => 31
                    [5] => 
                    [6] => 
                    [7] => 
                )

        )

    [4] => Array
        (
            [0] => Array
                (
                    [1] => 
                    [2] => 
                    [3] => 
                    [4] => 
                    [5] => 1
                    [6] => 2
                    [7] => 3
                )

            [1] => Array
                (
                    [1] => 4
                    [2] => 5
                    [3] => 6
                    [4] => 7
                    [5] => 8
                    [6] => 9
                    [7] => 10
                )

            [2] => Array
                (
                    [1] => 11
                    [2] => 12
                    [3] => 13
                    [4] => 14
                    [5] => 15
                    [6] => 16
                    [7] => 17
                )

            [3] => Array
                (
                    [1] => 18
                    [2] => 19
                    [3] => 20
                    [4] => 21
                    [5] => 22
                    [6] => 23
                    [7] => 24
                )

            [4] => Array
                (
                    [1] => 25
                    [2] => 26
                    [3] => 27
                    [4] => 28
                    [5] => 29
                    [6] => 30
                    [7] => 
                )

        )

    [5] => Array
        (
            [0] => Array
                (
                    [1] => 
                    [2] => 
                    [3] => 
                    [4] => 
                    [5] => 
                    [6] => 
                    [7] => 1
                )

            [1] => Array
                (
                    [1] => 2
                    [2] => 3
                    [3] => 4
                    [4] => 5
                    [5] => 6
                    [6] => 7
                    [7] => 8
                )

            [2] => Array
                (
                    [1] => 9
                    [2] => 10
                    [3] => 11
                    [4] => 12
                    [5] => 13
                    [6] => 14
                    [7] => 15
                )

            [3] => Array
                (
                    [1] => 16
                    [2] => 17
                    [3] => 18
                    [4] => 19
                    [5] => 20
                    [6] => 21
                    [7] => 22
                )

            [4] => Array
                (
                    [1] => 23
                    [2] => 24
                    [3] => 25
                    [4] => 26
                    [5] => 27
                    [6] => 28
                    [7] => 29
                )

            [5] => Array
                (
                    [1] => 30
                    [2] => 31
                    [3] => 
                    [4] => 
                    [5] => 
                    [6] => 
                    [7] => 
                )

        )

    [6] => Array
        (
            [0] => Array
                (
                    [1] => 
                    [2] => 
                    [3] => 1
                    [4] => 2
                    [5] => 3
                    [6] => 4
                    [7] => 5
                )

            [1] => Array
                (
                    [1] => 6
                    [2] => 7
                    [3] => 8
                    [4] => 9
                    [5] => 10
                    [6] => 11
                    [7] => 12
                )

            [2] => Array
                (
                    [1] => 13
                    [2] => 14
                    [3] => 15
                    [4] => 16
                    [5] => 17
                    [6] => 18
                    [7] => 19
                )

            [3] => Array
                (
                    [1] => 20
                    [2] => 21
                    [3] => 22
                    [4] => 23
                    [5] => 24
                    [6] => 25
                    [7] => 26
                )

            [4] => Array
                (
                    [1] => 27
                    [2] => 28
                    [3] => 29
                    [4] => 30
                    [5] => 
                    [6] => 
                    [7] => 
                )

        )

    [7] => Array
        (
            [0] => Array
                (
                    [1] => 
                    [2] => 
                    [3] => 
                    [4] => 
                    [5] => 1
                    [6] => 2
                    [7] => 3
                )

            [1] => Array
                (
                    [1] => 4
                    [2] => 5
                    [3] => 6
                    [4] => 7
                    [5] => 8
                    [6] => 9
                    [7] => 10
                )

            [2] => Array
                (
                    [1] => 11
                    [2] => 12
                    [3] => 13
                    [4] => 14
                    [5] => 15
                    [6] => 16
                    [7] => 17
                )

            [3] => Array
                (
                    [1] => 18
                    [2] => 19
                    [3] => 20
                    [4] => 21
                    [5] => 22
                    [6] => 23
                    [7] => 24
                )

            [4] => Array
                (
                    [1] => 25
                    [2] => 26
                    [3] => 27
                    [4] => 28
                    [5] => 29
                    [6] => 30
                    [7] => 31
                )

        )

    [8] => Array
        (
            [0] => Array
                (
                    [1] => 1
                    [2] => 2
                    [3] => 3
                    [4] => 4
                    [5] => 5
                    [6] => 6
                    [7] => 7
                )

            [1] => Array
                (
                    [1] => 8
                    [2] => 9
                    [3] => 10
                    [4] => 11
                    [5] => 12
                    [6] => 13
                    [7] => 14
                )

            [2] => Array
                (
                    [1] => 15
                    [2] => 16
                    [3] => 17
                    [4] => 18
                    [5] => 19
                    [6] => 20
                    [7] => 21
                )

            [3] => Array
                (
                    [1] => 22
                    [2] => 23
                    [3] => 24
                    [4] => 25
                    [5] => 26
                    [6] => 27
                    [7] => 28
                )

            [4] => Array
                (
                    [1] => 29
                    [2] => 30
                    [3] => 31
                    [4] => 
                    [5] => 
                    [6] => 
                    [7] => 
                )

        )

    [9] => Array
        (
            [0] => Array
                (
                    [1] => 
                    [2] => 
                    [3] => 
                    [4] => 1
                    [5] => 2
                    [6] => 3
                    [7] => 4
                )

            [1] => Array
                (
                    [1] => 5
                    [2] => 6
                    [3] => 7
                    [4] => 8
                    [5] => 9
                    [6] => 10
                    [7] => 11
                )

            [2] => Array
                (
                    [1] => 12
                    [2] => 13
                    [3] => 14
                    [4] => 15
                    [5] => 16
                    [6] => 17
                    [7] => 18
                )

            [3] => Array
                (
                    [1] => 19
                    [2] => 20
                    [3] => 21
                    [4] => 22
                    [5] => 23
                    [6] => 24
                    [7] => 25
                )

            [4] => Array
                (
                    [1] => 26
                    [2] => 27
                    [3] => 28
                    [4] => 29
                    [5] => 30
                    [6] => 
                    [7] => 
                )

        )

    [10] => Array
        (
            [0] => Array
                (
                    [1] => 
                    [2] => 
                    [3] => 
                    [4] => 
                    [5] => 
                    [6] => 1
                    [7] => 2
                )

            [1] => Array
                (
                    [1] => 3
                    [2] => 4
                    [3] => 5
                    [4] => 6
                    [5] => 7
                    [6] => 8
                    [7] => 9
                )

            [2] => Array
                (
                    [1] => 10
                    [2] => 11
                    [3] => 12
                    [4] => 13
                    [5] => 14
                    [6] => 15
                    [7] => 16
                )

            [3] => Array
                (
                    [1] => 17
                    [2] => 18
                    [3] => 19
                    [4] => 20
                    [5] => 21
                    [6] => 22
                    [7] => 23
                )

            [4] => Array
                (
                    [1] => 24
                    [2] => 25
                    [3] => 26
                    [4] => 27
                    [5] => 28
                    [6] => 29
                    [7] => 30
                )

            [5] => Array
                (
                    [1] => 31
                    [2] => 
                    [3] => 
                    [4] => 
                    [5] => 
                    [6] => 
                    [7] => 
                )

        )

    [11] => Array
        (
            [0] => Array
                (
                    [1] => 
                    [2] => 1
                    [3] => 2
                    [4] => 3
                    [5] => 4
                    [6] => 5
                    [7] => 6
                )

            [1] => Array
                (
                    [1] => 7
                    [2] => 8
                    [3] => 9
                    [4] => 10
                    [5] => 11
                    [6] => 12
                    [7] => 13
                )

            [2] => Array
                (
                    [1] => 14
                    [2] => 15
                    [3] => 16
                    [4] => 17
                    [5] => 18
                    [6] => 19
                    [7] => 20
                )

            [3] => Array
                (
                    [1] => 21
                    [2] => 22
                    [3] => 23
                    [4] => 24
                    [5] => 25
                    [6] => 26
                    [7] => 27
                )

            [4] => Array
                (
                    [1] => 28
                    [2] => 29
                    [3] => 30
                    [4] => 
                    [5] => 
                    [6] => 
                    [7] => 
                )

        )

    [12] => Array
        (
            [0] => Array
                (
                    [1] => 
                    [2] => 
                    [3] => 
                    [4] => 1
                    [5] => 2
                    [6] => 3
                    [7] => 4
                )

            [1] => Array
                (
                    [1] => 5
                    [2] => 6
                    [3] => 7
                    [4] => 8
                    [5] => 9
                    [6] => 10
                    [7] => 11
                )

            [2] => Array
                (
                    [1] => 12
                    [2] => 13
                    [3] => 14
                    [4] => 15
                    [5] => 16
                    [6] => 17
                    [7] => 18
                )

            [3] => Array
                (
                    [1] => 19
                    [2] => 20
                    [3] => 21
                    [4] => 22
                    [5] => 23
                    [6] => 24
                    [7] => 25
                )

            [4] => Array
                (
                    [1] => 26
                    [2] => 27
                    [3] => 28
                    [4] => 29
                    [5] => 30
                    [6] => 31
                    [7] => 
                )

        )

)

So, now it's easy to create month table for every month you need using something like this

  function month2table($month, $calendar_array) {
    $ca = 'align="center"';
    $res = "<table cellpadding=\"2\" cellspacing=\"1\" style=\"border:solid 1px #000000;font-family:tahoma;font-size:12px;background-color:#ababab\"><tr><td $ca>Mo</td><td $ca>Tu</td><td $ca>We</td><td $ca>Th</td><td $ca>Fr</td><td $ca>Sa</td><td $ca>Su</td></tr>";
    foreach ($calendar_array[$month] as $month=>$week) {
      $res .= '<tr>';
      foreach ($week as $day) {
        $res .= '<td align="right" width="20" bgcolor="#ffffff">' . ($day ? $day : ' ') . '</td>';
        }
      $res .= '</tr>';
      }
    $res .= '</table>';
    return $res;
    }

Use these functions like

  $calarr = year2array(2011);
  echo month2table(1, $calarr); // January
  echo month2table(2, $calarr); // February
  ...
  echo month2table(12, $calarr); // December

..or put months in for loop.

So... e.g. for January 2011 in your browser you'll see this

enter image description here

Hope this helps.

我很OK 2024-11-05 02:23:51
$daysArr = array('Mon','Tue','Wed','Thu','Fri','Sat','Sun');
$monthtotdays= cal_days_in_month(CAL_GREGORIAN,date('m'),date("Y"));
$currdays=jddayofweek (cal_to_jd(CAL_GREGORIAN, date('m'),1, date("Y")) , 2 );
$currdaysval = 0;
echo "<table border=1px>";
echo "<tr>";
for($d=0;$d<=6;$d++){
echo "<td>". $daysArr[$d]."</td>";
if($daysArr[$d]==$currdays)  $currdaysval = $d;
}

echo "</tr>";
echo "<tr>";
if($currdaysval > 0 ){
echo '<td colspan="'.$currdaysval.'"> </td>';
}
for($i=1;$i<=$monthtotdays;$i++){
echo "<td>".$i."</td>";
if(($i+$currdaysval )%7 <= 0 ){
echo "</tr><tr>";
}
}
echo "</tr></table>"
$daysArr = array('Mon','Tue','Wed','Thu','Fri','Sat','Sun');
$monthtotdays= cal_days_in_month(CAL_GREGORIAN,date('m'),date("Y"));
$currdays=jddayofweek (cal_to_jd(CAL_GREGORIAN, date('m'),1, date("Y")) , 2 );
$currdaysval = 0;
echo "<table border=1px>";
echo "<tr>";
for($d=0;$d<=6;$d++){
echo "<td>". $daysArr[$d]."</td>";
if($daysArr[$d]==$currdays)  $currdaysval = $d;
}

echo "</tr>";
echo "<tr>";
if($currdaysval > 0 ){
echo '<td colspan="'.$currdaysval.'"> </td>';
}
for($i=1;$i<=$monthtotdays;$i++){
echo "<td>".$i."</td>";
if(($i+$currdaysval )%7 <= 0 ){
echo "</tr><tr>";
}
}
echo "</tr></table>"
黎夕旧梦 2024-11-05 02:23:51

最佳答案有误!如果年份是闰年,则应该在周期之前添加一个条件,否则第一个月第一天的输出将会出现问题 查看屏幕截图

必要条件: date("L", mktime(0,0,0, 7,7, $year)) ? $天数 = 366 : $天数 = 365;
(如果年份是闰年,则周期计数器 = 366,否则为 365)

The best answer has an inaccuracy! You should add a condition if year is leap-year BEFORE a cycle, otherwise you will have problems with output of first days of first month see screenshot.

The necessary condition: date("L", mktime(0,0,0, 7,7, $year)) ? $days = 366 : $days = 365;
(if year is leap-year then counter of cycle = 366 else 365)

半衬遮猫 2024-11-05 02:23:51

将当前代码包装在函数中,然后将带有所需日期的参数传递给它。

Wrap your current code within a function then pass an argument to it with your desired date.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文