Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8734547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:51:45+00:00 2026-06-13T09:51:45+00:00

I have created the following function which prints a calendar with day starting from

  • 0

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;

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T09:51:46+00:00Added an answer on June 13, 2026 at 9:51 am

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a printf -like function which takes the following arguments: One mandatory
I have the following function which creates a std::vector of iterators into another container:
All, I have the following array and function, which will create a multidimensional array
I have created the following program which allows a user to guess a word
i have created custom query which i am sending via POST ajax but problem
I have a function which is merging certain files from a directory def merge(path):
I have the following function: CREATE FUNCTION [dbo].[ListStockBySubCategory] ( @CategoryID varchar(10), @SubCategoryID varchar(10), @startRowIndex
I have the following function that takes a number like 5 and creates a
I have a strange one. Create a new form. Then add the following function
I have the following scalar function in MS SQL 2005: CREATE FUNCTION [dbo].[Distance] (

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.