1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Simple Calendar using PHP

Discussion in 'PHP' started by pradeep, Jul 20, 2006.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    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.

    PHP:
     <?php
     
    require_once 'Calendar/Month/Weekdays.php';
     
     
    $Month = new Calendar_Month_Weekdays(date('Y'), date('n'));
     
     
    $Month->build();
     
     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.

    PHP:
    <?php
     $Day 
    = new Calendar_Day(20031023);
     
    $Uri = & new Calendar_Decorator_Uri($Day);
     
    $Uri->setFragments('year''month''day');
     echo 
    $Uri->prev('day');
     
    // Displays year=2003&month=10&day=22
     
    ?>
     

Share This Page