Php Draw Calendar - 5 days, 7 days

Posted on 2012-09-20
Last Modified: 2012-09-20
Hello php experts;

Having some trouble with this one.

I am trying to draw a calendar <Table> with an option to either draw a 5 day or 7 day calendar, with the 5 day, obviously excluding the weekends.

To make it even more interesting, I would like to include previous and next month dates to populate the "spaces", if there are any, at the beginning and end of the month.

For Example:
January 2013
31 01 02 03 04
07 08 09 10 11
14 15 16 17 18
21 22 23 24 25
28 29 30 31 01

Maybe this is simple and I am overlooking something.
Question by:theRounder
    LVL 107

    Expert Comment

    by:Ray Paseur
    Sounds like a custom app.  There are many calendars available with a Google search.

    See Practical Example #6 here:
    LVL 42

    Accepted Solution

    Have a look at fullCalendar. It's a jQuery based front end (displaying your calendar, view by day, week, month, forward, back etc) and can be hooked into a backend script to populate the calendar from a database, add new data to the database etc. You can choose to show weekends or not - Pretty nice.
    LVL 9

    Expert Comment

    by:Derek Jensen
    You've got 2 fundamental problems here:
    Finding the start day(must be done first), and
    looping through the remainder of the calendar days, based on user's selection.

    There are plethora ways to find the start day, so I'll leave that up to you. :-)

    Once you have the start day, if you wanted to display the days preceding the start day from the prior month, you'd need to determine the prior month, then how many days it has, then simply count backwards until you reach the start of the week.

    The start of the week would be determined, of course, by the user's selection of 5 or 7 day weeks. However, you're still going to loop through every single day of the month; this is the easiest way to guarantee the date remains correct, regardless of layout.

    If 5 day layout is chosen, you'd simply skip the weekend days.

    Here's the easiest way I see it, in simple pseudo:
    EndOfMonth = false;
    myDate = 0;
    TotalDays = getDaysOfMonth(currMonth);
    while (!EndOfMonth) {
        foreach (weekday as day) {
            if (myDate >= TotalDays) {EndOfMonth = true;}
            // Other stuff here, including check if 5-day layout on weekdays

    Open in new window

    LVL 107

    Assisted Solution

    by:Ray Paseur
    skip the weekend days
    Easy to do.  Render the day of the week with date('D') and skip the days that start with 'S' ;-)
    LVL 9

    Expert Comment

    by:Derek Jensen
    There ya go! ^_^

    Author Closing Comment

    Thank you. Tried tons of google searched calendars and none came even close to Full calendar! Awesome find.

    Featured Post

    Live: Real-Time Solutions, Start Here

    Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

    Join & Write a Comment

    Introduction The Google Maps API offers various ways to draw dynamic and static maps.  Using a combination of PHP and JavaScript, you can draw active JavaScript maps that allow pan-and-zoom in the client browser window.  You can also draw "static" …
    This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
    In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …
    The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now