user1421214
user1421214

Reputation: 909

Choosing any weekday as a start day to show on the calendar

I have created the following function which prints a calendar with day starting from Sunday (to Saturday) ... but I want to be able to choose any day as the first day... eg. first day being Wednesday ... I tried but couldn't get it working ... Can you help me fix this?

I know how to manipulate days heading array to reflect this start day but the calendar days somehow get messed up.

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;

}

Upvotes: 0

Views: 1353

Answers (1)

David Passmore
David Passmore

Reputation: 6099

You need a value to edit $firstDayOfMonth based on the first day of the array (in this example i am starting on monday) using October 2012:

<?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;

}
?>

The /* IMPORTANT STATEMENT */ is key because the mktime() method creates the date based on Sunday being the first day of the week so this overrides it.

See Result Here: Link

Upvotes: 1

Related Questions