More Free Resources

In this tutorial you will learn how to build your own web based calendar using PHP. This calendar is made from two parts. On top there are the links to the previous and the next month, and below them is the calendar itself. It will show the selected month name with the year following and the days of the month bellow in a table view. You will need a good PHP editor to help you with your development or if you are experienced enough you can use a plain text editing software such as Notepad.

At the beginning we have to decide how we are going to pass our parameters. We can use either 'hidden' input fields in out html code or pass them in the URL. In this tutorial we will use the second approach. We are going to need two parameters - one for the "month" and one for the "year". We will also need an array with month names:

$cMonth and $cYear are used for the current month and year displayed on the calendar. For the "Previous" and "Next" links we will need the corresponding parameters. We set $prev_year and $next_year to the current one. Later we may have to change this but for now it is OK. We also have to set parameters for the next and previous months by adding and subtracting 1. Now is the catch. We have to check if our parameter has not gone over or down the limit. Since there are 12 months in a year if our parameter goes to 13 then it means that another year has passed by and we have to set our "month" parameter back to 1 ( January ) and add 1 to our "year" parameter. The other way around is when we go back in time and our "month" parameter goes to 0. Then we have to decrease our "year" parameter by 1 and set the month parameter to 12 ( December ). Now as we set our links for previous and next months we turn to how to build the actual calendar.

We create a table that will hold our calendar and add the links in one row. Then we add a table in a new row that will hold the days. We also include the month name and the year in the first on the new table. But because arrays are zero based, we need to subtract one from the "month" parameter value to get the correct name.

Now we have to set the proper dates for our calendar. We have to make integer representation of the date so we can easily operate with it. Then we get the number of days of the selected month and the number representation (0 for Sunday through 6 for Saturday) of the first day of the month. All of these functions are fully explained in PHP Manual. Our loop that is going to print the dates starts at 0, because the days of the week start from 0 (Sunday). It has to loop through the number of days plus the offset of the first day of the month. We have to print new row for each week. We check this by modulus of the number of days in one week 7. If it equals 0 then it is the beginning of the week and we print open row tag <tr> and if it is the end of the week 6 we print close tag for this week </tr>. All we need to do is to check if the day that we print is before $startday. In this case we print empty tag. Otherwise we have to print the date. We make it by subtracting the $startday. we have to add one because we don't want our dates to start from 0.

140 Comments to "How to make a PHP calendar"

I know this Calendar was created a long time ago and I thank the original Author and others for their work.

What I'm trying to do is loop data to get multiple events per day, Im setting this up on my amateur radio site and I have multiple nets that I have to attend to for Emergency Communications. Some days its 4 to 5 nets withing 3 hours. I tried adding the nets to a data table and looping with a while statement but it only pulls one and displays it.
http://www.n7cjt.org/calendar.html

I want the weeks to to count themselves up, IE: it's week 26 of 2016, next week is week 27, etc, etc. I figured out the loop so thats taken care of, now I'm trying to figure out how to put the next week in the next weeks row

I got here via a help forum because I was assisting someone with this script.
They were having issues because they were trying to modify it but it kept messing up.
The problem was due to the fact this script uses 'shorthand' formatting for the IF() statements.
I know it's technically valid to omit the brackets, however, it's inappropriate to use shorthand code for a tutorial, unless that tutorial is about using shorthand code.

If you are going to post tutorials you should take into consideration that the people reading and doing those tutorials probably don't have the level of knowledge you do. Don't make their learning experience negative because you would rather take shortcuts.