Date/Time Processing with PHP - Checking Up
(Page 6 of 7 )
Options to getdate() include the localtime() and gettimeofday() functions, both of which return arrays containing time information. Take a look:
<?
// get current time as array
$current = gettimeofday();
// print it
print_r($current);
?>
This would generate the following output:
Array
(
[sec] => 1014189678
[usec] => 900172
[minuteswest] => -19800
[dsttime] => 0
)
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);
?>
Here's the output:
Array
(
[tm_sec] => 51
[tm_min] => 54
[tm_hour] => 12
[tm_mday] => 20
[tm_mon] => 1
[tm_year] => 102
[tm_wday] => 3
[tm_yday] => 50
[tm_isdst] => 0
)
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:
<?
// 31-Feb-2002 - false
echo checkdate(02, 31, 2002);
// 25-Dec-1956 - true
echo checkdate(12, 25, 1956);
// 31-Jun-2002 - false
echo checkdate(06, 31, 2002);
?>
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.
<?
// current values
$this->currYear = $year;
$this->currMonth = $month;
$this->currDay = date("j");
?>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.
<?
// if leap year, modify $totalDays array appropriately
if (date("L", mktime(0, 0, 0, $this->currMonth, 1,
$this->currYear)))
{
$this->totalDays[2] = 29;
}
?>Next: Turning The Tables >>
More PHP Articles
More By The Disenchanted Developer, (c) Melonfire