Home arrow PHP arrow Page 7 - Date/Time Processing with PHP

Turning The Tables - PHP

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.

TABLE OF CONTENTS:
  1. Date/Time Processing with PHP
  2. Getting A Date
  3. A Stamp In Time...
  4. Race Against Time
  5. When Looks Do Matter
  6. Checking Up
  7. Turning The Tables
By: The Disenchanted Developer, (c) Melonfire
Rating: starstarstarstarstar / 14
March 19, 2002

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
Now, let's get into the display() method, which takes care of actually printing the calendar. There's one important thing needed here: the day of the week on which the first of the month falls.

<?
// find out which day the first of the month falls on
$firstDayOfMonth = date("w", mktime(0, 0, 0,
$this->currMonth, 1, $this->currYear));
?>
The first day of the month (from the $firstDayOfMonth variable) and the last day (from the $totalDays array) provide the bounding values for the month view I'm going to be building.

I'll explain how this view is constructed row by row. The first row contains just the name of the current month.
<?
// 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";
?>
The next row contains seven cells, one for each day of the week - I've used the substr() function to display the first three letters of each day name from the $days array.
<?
// 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";
?>
The next few rows are all generated automatically. The first order of business is to place the first day of the month on the corresponding day. Since I already have a $firstDayOfMonth variable, I've used a simple loop to fill all the cells prior to that day with non-breaking spaces.
<?
// 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>&nbsp;</font></td>\n";
}
?>
The $rowCount variable is simultaneously keeping track of the number of slots (cells) being filled up - I'll use this a little further down to determine when the end of the week has been reached.

Once the first day of the month is determined, another "for" loop (iterating from 1 to the number of days in the month) is used to generate the remaining rows and cells of the table. The $rowCount and $dayCount variables are incremented at each stage, and the $rowCount variable is divided by 7 to find out when the seven slots available in each row are filled up.
<?
// 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";
}
?>
Notice that I've inserted an "if" statement into the loop to display the current date in a different colour, if a match is found.

Here's an example of how this class might be used:
<?
include("Calendar.inc");
$cal = new Calendar(2002, 12);
$cal->display();
?>
And here's what it would look like:



Pretty cool, huh?

This is just one example of the myriad uses to which PHP's date functions can be put. There are many more; I'll leave them to your imagination. Until next time...be good! Note: All examples in this article have been tested on Linux/i586 with Apache 1.3.20 and PHP 4.1.1. Examples are illustrative only, and are not meant for a production environment. Melonfire provides no warranties or support for the source code described in this article. YMMV!

 
 
>>> More PHP Articles          >>> More By The Disenchanted Developer, (c) Melonfire
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: