• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 610
  • Last Modified:

Php Draw Calendar - 5 days, 7 days

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.
2 Solutions
Ray PaseurCommented:
Sounds like a custom app.  There are many calendars available with a Google search.

See Practical Example #6 here:
Chris StanyonCommented:
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.

Derek JensenCommented:
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

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Ray PaseurCommented:
skip the weekend days
Easy to do.  Render the day of the week with date('D') and skip the days that start with 'S' ;-)
Derek JensenCommented:
There ya go! ^_^
theRounderAuthor Commented:
Thank you. Tried tons of google searched calendars and none came even close to Full calendar! Awesome find.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now