Video:How to Create a PHP Calendarwith Don Schechter
PHP calendars can be used for everything from simply displaying the date to creating complex reservation systems. See the steps to starting your own PHP calendar.See Transcript
Transcript:How to Create a PHP CalendarHi! I'm Don Schechter for About.com computing. Today I will show you how to create a simple calendar using PHP.
What Is a PHP Calendar?PHP calendars can be used for everything from simply displaying the date to creating complex reservation systems. Use your text editing program - I am using Notepad - and save your file with a .php extension
Create the Calendar VariablesLet's start with the php tag. Now we need to create four variables that will be used in a few moments.
Start by creating a variable for the current date. The dollar sign denotes a variable which you can give any name that you like. Let us keep it simple and call it date. Now add an equals sign followed by the word time, parenthesis, and a semicolon. This function will determine the time.
Set the PHP Calendar DatesNow we will use the date function to determine the day, month, and year. Create a day, month, and year variable. Next to each variable type an equal sign, the date function, parenthesis, and a semicolon.
PHP Calendar Day VariablesFor the day variable, within the parenthesis type the letter d in single quotes, followed by a comma and dollar sign date. This uses the time function which we set as our date variable to format our variables for day month and year.
PHP Calendar Month and Year VariablesFor the month variable, type the same thing but replace the d with an m. For the year variable use a Y
Now use the following code to generate the first day of the month. $first_day = mktime(0,0,0,$month, 1, $year) ;
To get the name of the month we need to type
$title = date('F', $first_day) ;
Now, we want to find out what day of the week the first day of the month falls on. So we need to include
$day_of_week = date('D', $first_day) ;
Set the Calendar Day VariablesIn a calendar, any days in the week prior to the start of the month need to be blank. If the first day is Sunday there are no blank spaces. If it is a Friday there are five blanks, and so on. We have to add the following chunk of code. Use the switch function calling the day of week variable. If it's a Sunday, the variable blank will be assigned a 0. If it is a Monday the blank variable will be assigned a one and so on.
case "Sun": $blank = 0; break;
case "Mon": $blank = 1; break;
case "Tue": $blank = 2; break;
case "Wed": $blank = 3; break;
case "Thu": $blank = 4; break;
case "Fri": $blank = 5; break;
case "Sat": $blank = 6; break;
Set the Month's DaysNow we need to determine how many days are in the current month. To do that we set a days in month variable to equal cal_days_in_month function with, in parenthesis, a 0 comma the month variable comma the year variable. Do not forget the semicolon.
Now we will start building the table by using echo to include table tags. Let us create a table with a set border and width and include the title and year variables. Now we need to label the days of the week by adding either a S for Sunday, M for Monday and so on for each of the columns of the calendar.
We need to count the days in each week so I will create a day count variable and set it to 1. And follow that up with an echo of a table row.
Loop the CalendarNow we will start a while loop to deal with the blank days prior to the start of the month. While our blank variable is greater than zero we will echo blank table data. We also need to decrease our blank count by one and increase our day count.
Now we set a day_num variable to 1, the first day of the month. We need a while loop to count up the days of the month, echo the day of the month and increase our day count until the month ends.
To create a new row we need an if statement. If the day_count variable is greater than 7 then we need to create a new row and set the day count to 1 and start a new week.
Now we have another while loop that will fill in blank days at the end if necessary. While the day count is greater than 1 and less than 7 we will echo empty table data and increase the day count.
View the PHP CalendarFinally, do not forget to close the table and end our code. I will save the file and upload it to my server. I will type in the URL and there is my calendar.
Thanks for watching. For more information, visit us online at computing.about.com.