Wednesday, December 17, 2008

How to make a Calendar in PHP?

Calendar using php code is to be made. This calendar shows one month and has previous and next buttons on it, which when clicked opens the calendar of previous and next month.

The code for this is written in notepad. Below is the php file.




First an array is made, which puts the month names in a variable named ‘month names’. Then some variables are named like previous month, next month, previous year and next year. In previous month variable, current month minus one is given, cMonth is the current month. If loop is started, if the next month is 13, it means it is the 1st month of a new year.

 This is the continued coding of the first one.


Then a table of calendar is to be made.In this we give its heading as 'Calendar'.Table row,header tags are started, some alignment is also done.

Then a table of calendar is to be made. In this we give its heading as 'Calendar’. Table row, header tags are started, some alignment is also done.

Two links naming previous and next are made with href tag. All tags opened are closed.

‘Tr’ tag is started, in which there are various td tags, and these td tags are for table data.6 td tags are opened for Sunday to Saturday. These are aligned in center, bgcolors and style color is also given. We can use different bgcolors.

The ‘mktime’ function used here returns the UNIX timestamp for a specified date.

Then a for loop is started if the variable ‘i’ is less than the starting day, it will return the current day, otherwise +1 day to the starting day.

 

So write this code in notepad and put into htdocs folder of xampp server.And run on your localhost by giving the address:

http://localhost/calend.php

This will show the following output:



This is the December month and had Previous and Next links on it, which opens the following window: These are the next months and previous month. So in php to make calendar is simple. It can be modified also by adding some colors in the table.




No comments:

Post a Comment