选择任何工作日作为在日历上显示的开始日期

时间:2012-10-26 10:00:49

标签: php

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

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

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;

}

1 个答案:

答案 0 :(得分:1)

你需要一个值来根据数组的第一天(在本例中我是星期一开始)使用2012年10月来编辑$firstDayOfMonth

<?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()方法根据星期日是一周的第一天创建日期,所以这会覆盖它。

请参见结果:Link