Go4Expert (http://www.go4expert.com/)
-   PHP (http://www.go4expert.com/articles/php-tutorials/)
-   -   Simple Calendar using PHP (http://www.go4expert.com/articles/simple-calendar-using-php-t1032/)

pradeep 20Jul2006 11:27

Simple Calendar using PHP
There are times when we require to put a calendar in some web appplication of ours,writing the code to generate a calendar from scratch is painstaking and waste of time. So PEAR comes to rescue with its Calendar package. The package gives a ton of features, below you'll find a simple program to generate the calendar of the present month.

Code: PHP

 require_once 'Calendar/Month/Weekdays.php';
 $Month = new Calendar_Month_Weekdays(date('Y'), date('n'));
 echo "<table border=1>\n";
 while ($Day = $Month->fetch()) {
     if ($Day->isFirst()) {
         echo "<tr>\n";
     if ($Day->isEmpty()) {
         echo "<td> \n";
     } else {
         echo '<td>'.$Day->thisDay()."\n";
     if ($Day->isLast()) {
         echo "</tr>\n";
 echo "</table>\n";

The calendar package also contains a Decorator class, which provides a decorator to help with generating URLs (for example next / prev URLs). Checkout an example below.

Code: PHP

 $Day = new Calendar_Day(2003, 10, 23);
 $Uri = & new Calendar_Decorator_Uri($Day);
 $Uri->setFragments('year', 'month', 'day');
 echo $Uri->prev('day');
 // Displays year=2003&month=10&day=22

All times are GMT +5.5. The time now is 14:23.