How to Customize a Calendar in PHP

By eHow Internet Editor

Rate: (1 Ratings)

Calendars are very useful, small widgets that you can put on your site. Calendars can be used for many more things than showing the current date. One of the most popular uses of a calendar control is to have the dates as working links: when a user clicks on a date, she will see the articles posted on that day. There are many open source PHP calendar controls available on the Internet. Of course, you can always create your own control, but it is often better to use a ready-made one. Almost all of the available controls are free for non-commercial use (many of them are free for any use, including commercial) and can be customized to your liking.

Instructions

Difficulty: Moderately Challenging

Things You’ll Need:

  • PHP IDE, or at least a text editor
  • MySQL database server, configured for work with PHP
  • PHP 5
  • Web server

Customize Functionality

Step1
Include the calendar functionality in your application by writing this:
require_once( 'class.Calendar.php' );
$cal = new Calendar ('2007', '8' );
echo "".$cal->getFullMonthName()."";
echo $cal->display();

These lines of code create a new instance of the calendar and display it in the browser. The calendar which will be generated in this example will be for August 2007, but you can easily change the month and year.
Step2
Opt for the abbreviated month name (like "Aug" instead of "August") by using the getBriefMonthName() function instead of the getFullMonthName() function.
Step3
Change the displayed day names by modifying the dspDayName function and including the new days in the array:
$names = array('2007-08-06','2007-08-07','2007-08-08','2007-08-09'
,'2007-08-10','2007-08-11','2007-08-12',);

Customize the Layout

Step1
Understand that one of the layout components you can customize for the PHP class calendar is the width of the table that will host it. You can set the width in pixels or in percentages. For instance, to set the width of the calendar to be 100 percent of the width of the containing cell, use the following:
$calendar->setTableWidth('100%');
Step2
Change the day name format. The possible choices are %a for abbreviated (such as Mon and Tu for Monday and Tuesday), %A for full (Monday, Tuesday and so on) or %u for the day's number (1 for Monday, 2 for Tuesday and so on). To set the date format to its abbreviation, use the following:
$calendar->setDayNameFormat('%a');
Step3
Make use of stylesheets. Since the calendar is displayed in a table, most of its appearance can be controlled via a stylesheet. By using a stylesheet, you can set such fundamental properties as font typeface, size and color, table borders and cell backgrounds.

Tips & Warnings

  • Depending on the control you have selected, you might be able to customize different aspects of its functionality and layout. The most important functions in a control are usually the day on which the week starts (Sunday or Monday), how dates are written (only as a number or together with the month and year) and what information is included in the headers.
  • Don't forget to customize the colors in the PHP control so that it uses the same colors as the rest of your website.
  • When using ready-made components, make sure that the control you are customizing is for the PHP version you are using for your project. PHP 3 and 4 are already obsolete, so chances are that your project, your provider and the control itself are using PHP 5 or higher.

Post a Comment

POST A COMMENT

Request a New How-To Article

Looking for more How To information? Chances are there’s an eHow member who knows how to do what you’re looking to do. Submit an article request now!

eHow Article:  How to Customize a Calendar in PHP

eHow Internet Editor

eHow Internet Editor

Category: Internet

Articles: See my other articles

Related Ads

Internet

Veesites
Meet Virginia DeBolt eHow’s Internet Expert.