PHP
  Home arrow PHP arrow Page 7 - Date/Time Processing with PHP
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Date/Time Processing with PHP
By: The Disenchanted Developer, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 11
    2002-03-19

    Table of Contents:
  • Date/Time Processing with PHP
  • Getting A Date
  • A Stamp In Time...
  • Race Against Time
  • When Looks Do Matter
  • Checking Up
  • Turning The Tables

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Date/Time Processing with PHP - Turning The Tables


    (Page 7 of 7 )

    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!
    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

     

       

    PHP ARTICLES

    - Validating Web Forms with the Code Igniter P...
    - Output Buffering
    - Paginating Database Records with the Code Ig...
    - HTTP Headers in Web Development
    - Project Management: Administration
    - Building a Database-Driven Application with ...
    - User Authentication for a Project Management...
    - Introduction to the CodeIgniter PHP Framework
    - Adding Users for a Project Management Applic...
    - Migrating Class Code for a MIME Email to PHP...
    - Login and Logout Authentication for a Projec...
    - Composing Messages in HTML for MIME Email wi...
    - Project Management: Authentication
    - A Better Way to Determine MIME Types for MIM...
    - Project Management Overview





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway