Like most programming languages, PHP comes with a fairlyfull-featured API for date and time value manipulation. You've probablyused it in your applications, but never bothered to look too closely atit. Well, here's your chance to rectify that mistake - this articledelves into the date/time API in depth, uncovering some hidden nuggetsand demonstrating how it can be used to simplify date and timeprocessing in your PHP scripts.
Alternatively, you can use the localtime() function,
which calls the system's localtime() function and returns the output as an array containing information similar to that returned by getdate(). Take a look:
<?
// get current time as array
// the second parameter tells PHP to create an associative array // omit
it to create a regular integer-indexed array $current =
localtime(mktime(), TRUE);
// print it
print_r($current);
?>
It's unlikely that you'll ever use these, since most
of what they provide is already available via the functions discussed previously. However, they're included here in case you ever have a need for this specialized information.
Finally, the checkdate() function tells you whether a given date is valid or not. Consider the following examples:
This function comes in particularly handy if you need to
validate date information entered into an online form - simply run the datestamp via checkdate() to see whether or not it's valid.{mospagebreak title=Back to Class} Now, how about doing something useful with all this information? This next example uses everything you've learned to construct a simple PHP class that prints a monthly calendar. Take a look:
<?
class Calendar
{
//
// class variables
//
// list of names for days and months
var $days = array("Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday");
var $months = array("", "January", "February", "March", "April",
"May", "June", "July", "August", "September", "October", "November",
"December");
// number of days in each month
var $totalDays = array(0, 31, 28, 31, 30, 31, 30, 31, 31, 30,
31, 30, 31);
// variables to hold current month, day and year
var $currYear;
var $currMonth;
var $currDay;
//
// class methods
//
// constructor
function Calendar($year, $month)
{
// current values
$this->currYear = $year;
$this->currMonth = $month;
$this->currDay = date("j");
// if leap year, modify $totalDays array appropriately
if (date("L", mktime(0, 0, 0, $this->currMonth, 1,
$this->currYear)))
{
$this->totalDays[2] = 29;
}
}
// this prints the HTML code to display the calendar
function display()
{
// find out which day the first of the month falls on
$firstDayOfMonth = date("w", mktime(0, 0, 0,
$this->currMonth, 1, $this->currYear));
// start printing table
echo "<table border=0 cellpadding=2 cellspacing=5>\n";
// header
echo "<tr>\n";
echo "<td colspan=7 align=center><font face=Arial
size=-1><b>" . $this->months[$this->currMonth] . " " . $this->currYear .
"</b></font></td>\n";
echo "</tr>\n";
// day names
echo "<tr>\n";
for ($x=0; $x<7; $x++)
{
echo "<td><font face=Arial size=-2>" .
substr($this->days[$x],0,3) . "</font></td>\n";
}
echo "</tr>\n";
// start printing dates
echo "<tr>\n";
// display blank spaces until the first day of the month
for ($x=1; $x<=$firstDayOfMonth; $x++)
{
// this comes in handy to find the end of each
7-day block
$rowCount++;
echo "<td><font face=Arial
size=-2> </font></td>\n";
}
// counter to track the current date
$dayCount=1;
while ($dayCount <= $this->totalDays[$this->currMonth])
{
// use this to find out when the 7-day block is
complete and display a new row
if ($rowCount % 7 == 0)
{
echo "</tr>\n<tr>\n";
}
// print date
// if today, display in different colour
if ($dayCount == date("j") && $this->currYear ==
date("Y") && $this->currMonth == date("n"))
{
echo "<td align=center
bgcolor=Silver><font face=Arial size=-1>$dayCount</font>";
}
else
{
echo "<td align=center><font face=Arial
size=-1>$dayCount</font>";
}
echo "</td>\n";
// increment counters
$dayCount++;
$rowCount++;
}
echo "</tr>\n";
echo "</table>\n";
}
// end of class
}
?>
Let's see how this works.
The first thing to do
is set a few class variables to hold the month and year to be displayed - these variables will be used throughout the class, and are crucial to it functioning correctly.
<?
// list of names for days and months
var $days = array("Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday");
var $months = array("", "January", "February", "March", "April",
"May", "June", "July", "August", "September", "October", "November",
"December");
// number of days in each month
var $totalDays = array(0, 31, 28, 31, 30, 31, 30, 31, 31, 30,
31, 30, 31);
// variables to hold current month, day and year
var $currYear;
var $currMonth;
var $currDay;
?>
The values of the current year and month are set in the
constructor, and are obtained via user input when an instance of the class is created.
The date() function even lets you find out if the year
under consideration is a leap year - if it is, it's necessary to modify the $totalDays array for the month of February. Since the date() function only works on a correctly-formatted UNIX timestamp, the mktime() function is used to first convert the numeric month and year into an acceptable format.