Keith Devens .com |
Friday, May 16, 2008 | ![]() |
| "I am a leaf on the wind. Watch how I soar." – 'Wash' (Serenity, 2005) | ||
PHP Calendar (version 2.3)Introduction
Here's a very simple, but very complete, PHP script to print out a calendar. It does one thing, but does it well (which is how it should be). Neat bonus features include the ability to link certain days in the calendar to a web page - useful for a weblog. You can also assign stylesheet classes to specific days, and provide content for an individual day. To use this code, just save the script to your server, include() it in your code, and display the output of generate_calendar(). New features: Version 2.1 is now locale-aware. All day and month names are automatically generated specific to your locale. Version 2.2 allows you to choose what day your weeks start on. See examples in the examples section. FunctionsJust one: function generate_calendar($year, $month, $days = array(), $day_name_length = 3, $month_href = NULL, $first_day = 0, $pn = array())
generate_calendar returns a string containing the output HTML. $year and $month are obvious. However, a neat side effect of using PHP's mktime function internally is that generate_calendar does "date rounding". So, basically, if you try to do month 13 of 2002, it'll round to January of 2003. See the documentation for mktime. $days is an optional array that can contain information you want to specify for each day, including a location to link that day to, a stylesheet class for that day, and any content you want to appear for that day of the calendar. For example, an array passed for the days parameter might look something like this:
<?php
Each key corresponds to the day of the month, and each value is an array containing one to three elements. The first element is the location you want the day to link to, the second element is a space-separated list of classes (for stylesheets), and the third element is content that you want to appear in that day's cell, instead of just the day's number. $day_name_length (optional, defaults to 3) is the number of characters to show for the day name ("Monday", "Tuesday", etc.). If you choose zero, the day headings don't display at all, and if you choose >3 it displays the whole name of the day. $month_href is the location you'd like the month to be a link to (optional). $first day is the number of the day you want your weeks to start on. Sunday is 0, Monday is 1, etc. Defaults to 0, hence Sunday. $pn contains the links for the previous and next months to link to (hence, 'pn'). It should be in the following format: $pn = array('<'=>'prev-link','>'=>'next-link'); Note that it's not necessary that the key for next and previous be < (<) and > (>). They could also be things like ← (←) and → (→) or « («) and » (»). In addition, it's important that you define them in the order just shown (previous before next in the array). If you just want to have a previous link, just define the first. If you just want to have a next link, you must define the first with a NULL key and then define the second. If one of the "directions" is defined but the link field is blank, the key (such as < or >) will still be shown, but it won't be a link. ExamplesShow the current month
All color and style information is configurable with stylesheets. Specify the number of characters to show for day name
As you can see, it only shows the first two letters of each day heading. Highlight the current dayA few people have asked if it is possible to do this. The code is built to be extensible so you can do whatever you want with it.
Link or provide content for certain days
I added the "twenty-six" so I could show how the content field works. I've used the content field as part of a task manager script I wrote. Using this feature it was easy to give myself a month's view of my tasks. The content for each day was just the text of that day's tasks. In another view, using the stylesheet classes I was able to have each day be highlighted to be a certain color depending on the highest priority task for that day. So I've gotten a lot of use out of this code. It's important to note, however, that the content field will not be automatically HTML-escaped for you, while the link and CSS classes fields will be. That's to give you the most flexibility while putting content in your calendar, but you must make sure you escape your HTML yourself for that field. Locale-awareness (new in 2.1): DutchAs of version 2.1, this code is completely locale-aware. The day and month names are generated from your locale.
Spanish, weeks start on Monday (new in 2.2)
With previous and next links (new in 2.3)
Note that the arrows inherit whatever style you set for your caption (css class 'calendar-month'), but you can set styles for them specifically. They're in <span> tags with classes of "calendar-prev" and "calendar-next". NotesSome calendar scripts will print out an entire year for you. I found that kind of unnecessary, since it's real easy to do yourself, and if you do it yourself you can format it any way you want. Here's (basically) the code I use to print the year in my weblog archive:
<table style="margin: auto">
$weblog->printCalendar() does some things like retrieve the list of days that have entries before it calls generate_calendar to print out the calendar. Note that the function must be given a date on or after 1970 due to limitations in underlying libraries. Download the sourceGet the plain source code or view the colorized source code if you want pretty colors. SupportNo support is provided for this code, as I get too many e-mails from either PHP newbies who want me to teach them PHP, or from people who don't bother to try the examples I've provided. It's a shame I have to do this because most people who e-mail are genuinely polite. However, if you think there's a problem with the code (there is a known problem with character encodings with some non-English locale settings) or you'd just like to drop me a line, please feel free. HistoryVersion 2 released on Jan 10, 2004. Version 2.1 released on May 28, 2004. Made it completely locale-aware. Also updated the code to make it simpler and reflect my current coding style, which made it 12 lines shorter. Version 2.1b, released the same day, removes the unneccessary "dayofweek" class on the day headings. Version 2.2 released on July 12, 2004. You can now choose what day of the week your calendar considers the first day. The code now uses %w instead of %u in strftime() for compatibility with Windows, and uses htmlentities() to handle encoding issues with non-English locales. The code is also 15 lines shorter, yet does more! :) Version 2.3 released on Aug 22, 2004. You can now provide links for the previous and next months. Also, one important change is that the CSS class of the "Month Year" caption has changed from "month" to "calendar-month". Change your CSS appropriately. Page last edited: November 24, 2005 (utc) |
IndexA B C D E F G H I J L M N O P R S T U V W X All pagesABC
DEFGHIJLMNOP
RS
T
UVWXGenerated in about 0.072s. (Used 4 db queries) |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||