Support Forum Softcomplex logo
About Us  | Services  | Download  | Order  | Support  | |
| Search | Today's Posts | Stats | Board Rules | Not logged in [ Login | Register ]
go to bottom
Last active: Never

Printable Version | Subscribe | Add to Favourites   Post new thread
Author: Subject: PHP Calendar - How to Create a PHP Calendar
annyphp
Junior Member

Posts: 4
Registered: 11/29/2010
Location: American
Member Is Offline
posted on 12/2/2010 at 03:29 AM Reply With Quote
PHP Calendar - How to Create a PHP Calendar


In this tutorial I will show you how to create a calendar script in PHP. We will create a simple but good looking - easy customizable via CSS - calendar table.
Step 1.
First of all try to collect the necessary information which are important to display the actual month and highlight the actual day. Besides this we want to display the actual month and year informations as well. To do this we need 3 special days informations:
1. The actual day
2. The first day of the actual month
3. The last day of the actual month
With the above mentioned informations we can decide what day was the first day, how long is the month and of course which is the actual day.

Step 2.
To get the information mentioned in Step 1 we will use the PHP built in function: getdate(). Without parameters this function returns the actual day informations in an array as follows:
01 Array
02 (
03 [seconds] => 40
04 [minutes] => 58
05 [hours] => 21
06 [mday] => 17
07 [wday] => 2
08 [mon] => 6
09 [year] => 2003
10 [yday] => 167
11 [weekday] => Tuesday
12 [month] => June
13 [0] => 1055901520
14 )

To get the last day of the month with getdate we need to try to get the 0. day of the next month. So the code to get the information looks like this:
1 <?php
2 $today = getdate();
3 $firstDay = getdate(mktime(0,0,0,$today['mon'],1,$today['year']));
4 $lastDay = getdate(mktime(0,0,0,$today['mon']+1,0,$today['year']));
5 ?>

Step 3.
To display a calendar we need a table with 7 columns for the days of the week. The number of lines depending on the number of days and the first day of the month. However we need a header line with month and year information, a subheader line with the name of the days.
1 <?php
2 // Create a table with the necessary header informations
3 echo '<table>';
4 echo ' <tr><th colspan="7">'.$today['month']." - ".$today['year']."</th></tr>";
5 echo '<tr class="days">';
6 echo ' <td>Mo</td><td>Tu</td><td>We</td><td>Th</td>';
7 echo ' <td>Fr</td><td>Sa</td><td>Su</td></tr>';
8 ?>

Step 4.
Ok, now we have the header of the table. Let's try to fill the first row. It is not so easy as you can not just write 1 in the first cell, 2 in the second and so on. It only works if the first day of the month was Monday, but what if not? To decide this we need the wday item from the firstDay array. With this information we can fill the cells with a space if needed. The code to do this is the follows:
01 <?php
02 echo '<tr>';
03 for($i=1;$i<$firstDay['wday'];$i++){
04 echo '<td>&nbsp;</td>';
05 }
06 $actday = 0;
07 for($i=$firstDay['wday'];$i<=7;$i++){
08 $actday++;
09 echo "<td>$actday</td>";
10 }
11 echo '</tr>';
12 ?>



Step 5.
As next step we need to fill to following lines. It is a bit easier, we only need to know how many full week we have and fill some table rows as follows:
01 <?php
02 $fullWeeks = floor(($lastDay['mday']-$actday)/7);
03
04 for ($i=0;$i<$fullWeeks;$i++){
05 echo '<tr>';
06 for ($j=0;$j<7;$j++){
07 $actday++;
08 echo "<td>$actday</td>";
09 }
10 echo '</tr>';
11 }
12
13 ?>
Step 6.
As semi final step we need to add the rest of the month to the last line. In this case it is quite easy:
01 <?php
02 if ($actday < $lastDay['mday']){
03 echo '<tr>';
04 for ($i=0; $i<7;$i++){
05 $actday++;
06 if ($actday <= $lastDay['mday']){
07 echo "<td>$actday</td>";
08 }
09 else {
10 echo '<td>&nbsp;</td>';
11 }
12 }
13 echo '</tr>';
14 }
15 ?>
Step 7.
To make the calendar little bit nicer we will introduce some CSS design. The CSS file is very simple:
01 table {
02 width:210px;
03 border:0px solid #888;
04 border-collapse:collapse;
05 }
06 td {
07 width:30px;
08 border-collpase:collpase;
09 border:1px solid #888;
10 text-align:right;
11 padding-right:5px;
12 }
13 .days{
14 background-color: #F1F3F5;
15 }
16 th {
17 border-collpase:collpase;
18 border:1px solid #888;
19 background-color: #E9ECEF;
20 }
21 .actday{
22 background-color: #c22;
23 font-weight:bold;
24 }

Step 8.The complete code using the CSS is the following:

01 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
02 <html>
03 <head>
04 <link href="style/style.css" rel="stylesheet" type="text/css" />
05 </head>
06 <body>
07 <?php
08 function showCalendar(){
09 // Get key day informations.
10 // We need the first and last day of the month and the actual day
11 $today = getdate();
12 $firstDay = getdate(mktime(0,0,0,$today['mon'],1,$today['year']));
13 $lastDay = getdate(mktime(0,0,0,$today['mon']+1,0,$today['year']));
14
15 // Create a table with the necessary header informations
16 echo '<table>';
17 echo ' <tr><th colspan="7">'.$today['month']." - ".$today['year']."</th></tr>";
18 echo '<tr class="days">';
19 echo ' <td>Mo</td><td>Tu</td><td>We</td><td>Th</td>';
20 echo ' <td>Fr</td><td>Sa</td><td>Su</td></tr>';
21
22 // Display the first calendar row with correct positioning
23 echo '<tr>';
24 for($i=1;$i<$firstDay['wday'];$i++){
25 echo '<td>&nbsp;</td>';
26 }
27 $actday = 0;
28 for($i=$firstDay['wday'];$i<=7;$i++){
29 $actday++;
30 if ($actday == $today['mday']) {
31 $class = ' class="actday"';
32 } else {
33 $class = '';
34 }
35 echo "<td$class>$actday</td>";
36 }
37 echo '</tr>';
38
39 //Get how many complete weeks are in the actual month
40 $fullWeeks = floor(($lastDay['mday']-$actday)/7);
41 for ($i=0;$i<$fullWeeks;$i++){
42 echo '<tr>';
43 for ($j=0;$j<7;$j++){
44 $actday++;
45 if ($actday == $today['mday']) {
46 $class = ' class="actday"';
47 } else {
48 $class = '';
49 }
50 echo "<td$class>$actday</td>";
51 }
52 echo '</tr>';
53 }
54
55 //Now display the rest of the month
56 if ($actday < $lastDay['mday']){
57 echo '<tr>';
58 for ($i=0; $i<7;$i++){
59 $actday++;
60 if ($actday == $today['mday']) {
61 $class = ' class="actday"';
62 } else {
63 $class = '';
64 }
65
66 if ($actday <= $lastDay['mday']){
67 echo "<td$class>$actday</td>";
68 }
69 else {
70 echo '<td>&nbsp;</td>';
71 }
72 }
73 echo '</tr>';
74 }
75 echo '</table>';
76 }
77 showCalendar();
78 ?>
79 </body>
80 </html>
More how to creat PHP Calendar you can view:
View User's Profile View All Posts By User U2U Member
Post new thread

Related Links:
Product Page
Product Demonstrations Live Demos
Free/Trial Script Download Free Download
Product Documentation Documentation
Documentation page
Change Log
Support Forum Support Forum
Order Now ORDER NOW!
Add to Cart
View Cart
 

Go To Top


Powered by XMB 1.9.1 Nexus
Developed By Aventure Media & The XMB Group © 2002-2004