//Finaly we finish out the table with some blank details if needed
while ( $day_count >1 && $day_count <=7 )
{
echo " ";
$day_count++;
}
echo ""; To finish our calendar we use one last while loop. This one fills in the rest of our calendar with blank table details if needed. Then we close our table and our script is done.
Friday, April 23, 2010
Simple PHP Calendar - Finishing the Calendar
Simple PHP Calendar - Days of the Month
//sets the first day of the month to 1
$day_num = 1;
//count up the days, untill we've done all of them in the month
while ( $day_num <= $days_in_month )
{
echo " $day_num ";
$day_num++;
$day_count++;
//Make sure we start a new row every week
if ($day_count > 7)
{
echo "";
$day_count = 1;
}
} Now we need to fill in the days of the month. We do this with another while loop, but this time we are counting up to the last day of the month. Each cycle echos a table detail with the day of the month, and it repeats until we reach the last day of the month. Our loop also contains a conditional statement. This checks if the days of the week have reached 7, the end of the week. If it has, it starts a new row, and resets the counter back to 1 (the first day of the week).
Simple PHP Calendar - Headings and Blank Calendar Days
//Here we start building the table heads
echo "| $title $year | ||||||
|---|---|---|---|---|---|---|
| S | M | T | W | T | F | S |
| "; $blank = $blank-1; $day_count++; } The first part of this code very simply echos the table tags, the month name, and the headings for the days of the week. Then we start a while loop. What we are doing is echoing empty table details, one for each blank day we count down. Once the blank days are done it stops. At the same time, our $day_count is going up by 1 each time through the loop. This is to keep count so that we do not try to put more than seven days in a week. | ||||||
Simple PHP Calendar - Days of the Week
//Here we find out what day of the week the first day of the month falls on
$day_of_week = date('D', $first_day) ;
//Once we know what day of the week it falls on, we know how many blank days occure before it. If the first day of the week is a Sunday then it would be zero
switch($day_of_week){
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;
}
//We then determine how many days are in the current month
$days_in_month = cal_days_in_month(0, $month, $year) ;
Here we take a closer look at the days of the month and prepare to make our calendar table. The first thing we do is determine what day of the week the first of the month falls. Once we know that, we use the switch () function to determine how many blank days we need in our calendar before the first day. Next we count the total days of the month. Now that we know how many 'blank' days we need, and how many total days are in the month we can start to generate our calendar.
Simple PHP Calendar - Getting Calendar Variables
PHP calendars can be very useful. You can do things as simple as showing the date, and as complex as an online booking system. In this tutorial we will show you how to generate a very simple PHP calendar. Once you understand how to do this, you will be able to apply the same concepts to more complex calendars you may need.
//This gets today's date
$date =time () ;
//This puts the day, month, and year in seperate variables
$day = date('d', $date) ;
$month = date('m', $date) ;
$year = date('Y', $date) ;
//Here we generate the first day of the month
$first_day = mktime(0,0,0,$month, 1, $year) ;
//This gets us the month name
$title = date('F', $first_day) ;
In the first part of our code we are setting some variables we will need later in the script. First we find out what today's date is using the time () function. We then use the date () function to format our date appropriately for the $day, $month, and $year variables. Finally we use it to generate the name of the month, which will be the title of our calendar.
Learn PHP Part 1
The essentials for learning PHP:
Learning a new language (programming or otherwise) can be a bit overwhelming. Many people just don't know where to start and give up before they even begin. Learning PHP is NOT as overwhelming as it might seem, I promise you. Just take it one step at a time, and before you know it you'll be off and running.1.) Basic Knowledge:
The first thing you need, before you start learning PHP, is a basic understanding of HTML. You can switch between PHP and HTML right in the same document. You can even run PHP from an HTML file.2.) Tools:
When creating PHP pages, you can use the same program you use to create your HTML pages. Any plain text editor will do. You will also need an FTP client to transfer files from your computer to your web hosting. If you already have an HTML website you most likely already use an FTP program.3.) The Basics:
Now you can finally get started learning PHP! The first thing you should read is our PHP Basics tutorial. This will take you through creating your first file, using variables, basic math, and basic IF statements (a form of logic).4.) Learning Loops :
Once you have mastered these skills, it is time to learn about loops. Loops repeat the same actions over and over again until a condition is met. There are several different types of loops which are explained in our Learning Loops tutorial.5.) PHP Functions:
Finally, you can learn to write your own custom functions. From here the sky is the limit... With a solid knowledge of these PHP basics, adding PHP functions to your arsenal when you need them will be easy.
Subscribe to:
Posts (Atom)
