In this fourth part of an eight-part article series on working with date and time functions in PHP, you'll get an introduction to the Calendar package, which can help you handle many chronological tasks automatically. This article is excerpted from chapter 12 of the book Beginning PHP and PostgreSQL 8: From Novice to Professional, written by W. Jason Gilmore and Robert H. Treat (Apress; ISBN: 1590595475).
Creating a Calendar
The Calendarpackage consists of 12 classes capable of automating numerous chronological tasks. The following list highlights just a few of the useful ways in which you can apply this powerful package:
Render a calendar of any scope (hourly, daily, weekly, monthly, and yearly being the most common) in a format of your choice.
Navigate calendars in a manner reminiscent of that used by the Gnome Calendar and Windows Date & Time Properties interface.
Validate any date. For example, you can useCalendarto determine whether April 1, 2019 falls on a Monday (it does).
ExtendCalendarís capabilities to tackle a variety of other tasks, date analysis for instance.
In this section, youíll learn aboutCalendarís most important capabilities, followed by several examples showing you how to actually implement some of these interesting features. But before you can begin taking advantage of this powerful package, you need to install it. Although you learned all about the PEAR package installation process in Chapter 11, for those of you not yet entirely familiar with the installation process, the necessary steps are reproduced next.
To capitalize upon all ofCalendarís features, you also need to install theDatepackage. Letís take care of both during theCalendarinstallation process, which follows:
%>pear install Date downloading Date-1.4.3.tgz ... Starting to download Date-1.4.3.tgz (42,048 bytes) ............done: 42,048 bytes install ok: Date 1.4.3 %>pear install -f Calendar Warning: Calendar is state 'beta' which is less stable than state 'stable' downloading Calendar-0.5.2.tgz ... Starting to download Calendar-0.5.2.tgz (60,164 bytes) ..............done: 60,164 bytes Optional dependencies: package `Date' is recommended to utilize some features. install ok: Calendar 0.5.2 %>
The -fflag is included when installingCalendarhere because, at the time of this writing,Calendaris still a beta release. By the time of publication,Calendar could be officially stable, meaning you wonít need to include this flag. See Chapter 11 for a complete introduction to PEAR and theinstallcommand.