Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function split() is deprecated in /home3/vvaswani/public_html/community/columns/trog/article.php on line 101

Deprecated: Function eregi_replace() is deprecated in /home3/vvaswani/sql.php on line 301

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home3/vvaswani/public_html/community/columns/trog/article.php on line 107

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home3/vvaswani/public_html/community/columns/trog/article.php on line 109

Deprecated: Function ereg_replace() is deprecated in /home3/vvaswani/public_html/community/columns/trog/article.php on line 124

Deprecated: Function ereg_replace() is deprecated in /home3/vvaswani/public_html/community/columns/trog/article.php on line 125

Deprecated: Function split() is deprecated in /home3/vvaswani/public_html/community/columns/trog/article.php on line 127

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174
The Melonfire Community - Trog
Logo         Community
  Trog
Services
The Company
Community
Columns
Your Account
Contact Us
 
 
Creating Web Calendars With The PEAR Calendar Class
Need a quick-and-dirty Web calendar? Look no further!

| A Month And A Year |

Before using Calendar in a project, it's important to visualize the type of calendar you want to display, and then use the appropriate object from Calendar's collection. Most of the time, you'll want to display the traditional monthly calendar, which looks like this:

Output image

Here's the code to accomplish this:


<html>
<head></head>
<body>
<pre>
<?php
// include class
include("Calendar/Month/Weekdays.php");

// create and build month data structure
// containing day objects
// classified by week
$month = & new Calendar_Month_Weekdays(2005, 6);
$month->build();

// print day names
echo " M  T  W  T  F  S  S";

// iterate over days
// display as weekly calendar
while ($day = $month->fetch()) {
    // if first day of week
    // start a new line
    if ($day->isFirst()) {
        echo "\r\n";
    }

    // if this day does not belong to this month
    // print empty spaces
    if ($day->isEmpty()) {
        echo "   ";
    // else print the day number
    } else {
        echo sprintf("%2d", $day->thisDay()) . " ";
    }
}
?>
</pre>
</body>
</html>


To support creation of the traditional monthly calendar, the Calendar package includes a special object, the Month_Weekdays object. This object, when initialized with a specific month and year, spawns Day objects for that month. Each of these Days comes with additional attributes indicating its week and month position. It's then possible to iterate over the object collection with fetch() and display the days in calendar format. The isFirst() method indicates whether the day is the first day of the week, while the isEmpty() method indicates whether the day belongs to the previous or next month.

Wanna do a calendar for the whole year? Just wrap the whole thing in a loop that runs twelve times:


<html>
<head></head>
<body>
<pre>
<?php
// include class
include("Calendar/Month/Weekdays.php");

// loop 12 times
// once for each month
for ($x=1; $x<13; $x++) {
    // create and build month data structure
    // containing day objects
    // classified by week
    $month = & new Calendar_Month_Weekdays(2005, $x);
    $month->build();

    // print month
    echo "       $x/2005 \r\n";
   
    // print day names
    echo " M  T  W  T  F  S  S";
   
    // iterate over days
    // display as weekly calendar
    while ($day = $month->fetch()) {
        // if first day of week
        // start a new line
        if ($day->isFirst()) {
            echo "\r\n";
        }
   
        // if this day does not belong to this month
        // print empty spaces
        if ($day->isEmpty()) {
            echo "   ";
        // else print the day number
        } else {
            echo sprintf("%2d", $day->thisDay()) . " ";
        }
    }
    echo "\r\n\r\n";
}
?>
</pre>
</body>
</html>


Here's what it looks like:

Output image


How to do Everything with PHP & MySQL
How to do Everything with PHP & MySQL, the best-selling book by Melonfire, explains how to take full advantage of PHP's built-in support for MySQL and link the results of database queries to Web pages. You'll get full details on PHP programming and MySQL database development, and then you'll learn to use these two cutting-edge technologies together. Easy-to-follow sample applications include a PHP online shopping cart, a MySQL order tracking system, and a PHP/MySQL news publishing system.

Read more, or grab your copy now!


previous page more like this  print this article  next page
 
Search...
 
In trog...
Logging With PHP
Building A Quick-And-Dirty PHP/MySQL Publishing System
Output Buffering With PHP
Date/Time Processing With PHP
Caching Web Sites With PEAR Cache
more...
 
In the hitg report...
Crime Scenes
Animal Attraction
Lord Of The Strings
more...
 
In boombox...
Patience - George Michael
Think Tank - Blur
My Private Nation - Train
more...
 
In colophon...
Hostage - Robert Crais
The Dead Heart - Douglas Kennedy
Right As Rain - George Pelecanos
more...
 
In cut!...
American Chai
The Core
Possession
more...
 
Find out how you can use this article on your own Web site!


Copyright © 1998-
Strict Standards: mktime(): You should be using the time() function instead in /home3/vvaswani/public_html/community/columns/trog/article.php on line 731
2017 Melonfire. All rights reserved
Terms and Conditions | Feedback