Dates are probably the most complex data types that programmers commonly have to deal with, and you will seldom if ever find
any quick miracles for working with them or performing date arithmetic. PHP, like most if not all modern programming languages,
reduces this complexity to timestamps internally, but even though PHP takes care of much of the overhead for you, turning
these into human-readable formats can still be a tricky proposition.
In the first part of this chapter, you looked at the most important and useful functions that PHP provides for displaying
dates and times, including the date(), time(), and mktime() functions. Also, certain functions exist for working with dates
and times in UTC as opposed to local or server time (gmdate() and gmmktime()), and you saw how to use these to convert between
local and UTC/GMT dates and times. In addition, PHP provides some ways to localize formatted dates and times (using the strftime()
function).
You also did some date arithmetic, taking advantage of some of the relative, ordinary-language arguments (such as +1 week
or −3 days) that can be used with the strtotime() function. This technique can be handy; it can take care of rollover issues
so you do not have to worry about what happens when, for instance, you add 12 hours to 8 p.m. and the resulting time is on
the following day.
However, PHP’s date and time functions have some drawbacks, in particular with regard to those used to output formatted dates.
In the first place, the arguments used for these are somewhat cryptic and can be difficult to remember. In addition, the formatting
characters used with local dates and times (used with date()) and those for locale-specific dates and times (those used as
arguments to strftime()) are not at all consistent with one another; in fact, the two sets of formatting characters do not
even come close to mapping to one another.
The second half of this chapter was devoted to taking care of the first of these problems by developing a couple of classes
to provide a clear and consistent means of working with dates that does not involve having to look up formatting characters,
many of which are not especially memorable but are easily confused with one another. The Date class implements a well-known interface that is defined in a recognized international
standard (ECMA-262), pro- viding the means to define Date instances and to set and to retrieve aspects of them (such as months
and hours) that are easily recognizable to human beings. Since this class in and of itself does not provide much flexibility
in formatting dates for output, you extended it in a DateExtended class that does a better job at making this functionality
available to the program- mer. These classes also simplify the tasks of converting between local time and UTC as well as other
time zones.
In wrapping things up, we also gave you some suggestions for taking the PHP 5 classes in this chapter and building on them
to handle issues such as extended date arithmetic. We also sketched the outline of a class you might want to write that would
extend Date and DateExtended for localization purposes. In any case, we have been using these classes and their predecessors
for a couple of years now in our own projects. They have let us handle many of the date and time tasks we have needed to accomplish
much more quickly and easily than by using the native PHP functions alone, and we hope you will also find them to be beneficial
in your own work and that they will serve as a basis you can build upon to meet your needs.