0

我创建了以下函数,它打印从星期日(到星期六)开始的日历......但我希望能够选择任何一天作为第一天......例如。第一天是星期三...我试过但无法正常工作...你能帮我解决这个问题吗?

我知道如何操纵天标题数组来反映这个开始日,但日历天不知何故搞砸了。

function testme() {
        $month = 8;
        $year = 2012;
        $days = array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");

    echo $firstDayOfMonth = date('w',mktime(0,0,0,$month,1,$year)); // a zero based day number
    $daysInMonth     = date('t',mktime(0,0,0,$month,1,$year));

    $calendar = ' <!-- start cal -->';
    $calendar = '<table border="1" class="calendar">'."\r\n";
    $calendar .= '<thead><tr><th class="calendar-day-head">'.implode('</th><th class="calendar-day-head">',$days ).'</th></tr></thead><tbody>';
    $calendar .= "\r\n".'<tr class="calendar-row">';
    $calendar .= str_repeat('<td class="calendar-day-np">&nbsp;</td>', $firstDayOfMonth); // "blank" days until the first of the current week
    $calendar .= '';

    $dayOfWeek = $firstDayOfMonth + 1; // a 1 based day number: cycles 1..7 across the table rows

    for ($dayOfMonth = 1; $dayOfMonth <= $daysInMonth; $dayOfMonth++)
    {
        $date = sprintf( '%4d-%02d-%02d', $year, $month, $dayOfMonth );

        $calendar .= '';
        $calendar .= '<td class="calendar-day">
            '.$dayOfMonth.' <br />';

        $calendar .= '';

        $calendar .= '</td>'."\r\n";
        if ($dayOfWeek >= 7)
        {
            $calendar.= '</tr>'."\r\n";
            if ($dayOfMonth != $daysInMonth)
            {
                $calendar .= '<tr  class="calendar-row">';
            }
            $dayOfWeek = 1;
        }
        else
        {
            $dayOfWeek++;
        }
    }
    //echo 8-$dayOfWeek;
    $calendar .= str_repeat('<td  class="calendar-day-np">&nbsp;</td>', 8 - $dayOfWeek); // "blank" days in the final week
    $calendar .= '</tr></table>';
    $calendar .= ' <!-- end cal -->';
    echo $calendar;

}

4

1 回答 1

1

您需要$firstDayOfMonth使用 2012 年 10 月根据数组的第一天(在此示例中我从星期一开始)编辑一个值:

<?php
function testme() {
        $month = 10;
        $year = 2012;
        $days = array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday");

    echo $firstDayOfMonth = date('w',mktime(0,0,0,$month,1,$year)); // a zero based day number


    /* IMPORTANT STATEMENT
       value based on the starting day of array
       E.G. (starting_day = value):
       Tuesday = 5
       Wednesday = 4
       Thursday = 3
       Friday = 2
       Saturday = 1
       Sunday = 0
       Monday = -1

    */

    $firstDayOfMonth = $firstDayOfMonth - 1;

    /* END IMPORTANT STATEMENT */


    $daysInMonth     = date('t',mktime(0,0,0,$month,1,$year));

    $calendar = ' <!-- start cal -->';
    $calendar = '<table border="1" class="calendar">'."\r\n";
    $calendar .= '<thead><tr><th class="calendar-day-head">'.implode('</th><th class="calendar-day-head">',$days ).'</th></tr></thead><tbody>';
    $calendar .= "\r\n".'<tr class="calendar-row">';
    $calendar .= str_repeat('<td class="calendar-day-np">&nbsp;</td>', $firstDayOfMonth); // "blank" days until the first of the current week
    $calendar .= '';

    $dayOfWeek = $firstDayOfMonth + 1; // a 1 based day number: cycles 1..7 across the table rows

    for ($dayOfMonth = 1; $dayOfMonth <= $daysInMonth; $dayOfMonth++)
    {
        $date = sprintf( '%4d-%02d-%02d', $year, $month, $dayOfMonth );

        $calendar .= '';
        $calendar .= '<td class="calendar-day">
            '.$dayOfMonth.' <br />';

        $calendar .= '';

        $calendar .= '</td>'."\r\n";
        if ($dayOfWeek >= 7)
        {
            $calendar.= '</tr>'."\r\n";
            if ($dayOfMonth != $daysInMonth)
            {
                $calendar .= '<tr  class="calendar-row">';
            }
            $dayOfWeek = 1;
        }
        else
        {
            $dayOfWeek++;
        }
    }
    //echo 8-$dayOfWeek;
    $calendar .= str_repeat('<td  class="calendar-day-np">&nbsp;</td>', 8 - $dayOfWeek); // "blank" days in the final week
    $calendar .= '</tr></table>';
    $calendar .= ' <!-- end cal -->';
    echo $calendar;

}
?>

/* IMPORTANT STATEMENT */是关键,因为 mktime() 方法根据星期日作为一周的第一天创建日期,因此它会覆盖它。

在此处查看结果:链接

于 2012-10-26T10:43:20.960 回答