#native_company# #native_desc#
#native_cta#

Really simple calendar

By anthony hersey
on September 6, 2000

Version: 1.1

Type: Full Script

Category: Calendars/Dates

License: Other

Description: Really simple calendar generator, it makes a calendar 3 months across and 4 down. Embedded within are 2 functions – one makes each month, and the other just loops through all the months. This is a PHP4 script, but it should be fairly simple to convert to PHP3 (hint: You’d have to initialize $sequence differently). There’s no comments, but it should be pretty obvious as to how it works, especially after reading up on the getdate() PHP function. License restricted to the use for the forces of good only.

See an example at http://www.superfudge.com/calendar.php

<?php

/*

This is a simple calendar generator, consisting of two functions.

   genYear() formats a table in a 12 unit grid, the arguments 
     are the year and columns, so 

         <?php echo genYear(2000,3); ?>

     will print out a 3x4 calendar.
     
   genMonth() formats subtables for each month. arguments are 
     year, month, and offset. the year and month can be entered 
     manually, but the genYear() function calls genMonth() with 
     the appropriate arguments. the offset is not necessary, but 
     allows for fancy formatting of the HTML output, which makes 
     flow control errors easier to spot.
     
         <?php echo genMonth(2000,9,"    "); ?>
         
     will print out a table for September 2000 with a 4 space 
     offset on every line of HTML.

   This page has a simple form that shows the script in action. 
     You may also see it on my site at:
   
         http://www.superfudge.com/calendar.php
   
   
I hope all of you  have found this useful. I didn't expect 
something I wrote in 5 minutes (originally, maybe like 45 now :) 
as a sideline to my weblog would be so popular. Hack to your 
heart's content! 

     - [email protected]

*/


function genMonth($year,$month,$offset) 
{
    $sequence = array_pad(array(), 43, "&nbsp");
    $monthstart = getdate(mktime(0,0,0,$month,1,$year));
    $monthend = getdate(mktime(0,0,0,($month + 1),0,$year));
    
    for ($i = 1; $i <= $monthend["mday"]; $i++)
    {
        $sequence[$i + $monthstart["wday"]] = $i;
    }
    
    $monthtable  = "$offset<table>n";
    $monthtable .= "$offset  <tr align=center>n";
    $monthtable .= "$offset    <td colspan=7>" . $monthstart["month"] . " " . $year ."</td>n";
    $monthtable .= "$offset  </tr>n";
    $monthtable .= "$offset  <tr align=center>n";
    $monthtable .= "$offset    <td>Sun</td><td>Mon</td><td>Tue</td><td>Wed</td><td>Thu</td><td>Fri</td><td>Sat</td>n";
    $monthtable .= "$offset  </tr>n";
    for ($i = 1; $i <= 36; $i += 7)
    {
        $monthtable .= "$offset  <tr align=right valign=bottom>n";
        for ($day = $i; $day <= $i + 6; $day++)
        {
            $monthtable .= "$offset    <td width=25 height=10>" . $sequence[$day] . "</td>n";
        }
        $monthtable .= "$offset  </tr>n";
    }
    $monthtable .= "$offset</table>n";
    
    return $monthtable;
}



function genYear($year,$cols)
{
    $validcols = array(1,2,3,4,6,12);
    if (!in_array($cols,$validcols))
    {
        $cols = 3;
    } 
    $yeartable  = "<table border=1>n";
    for ($i = 1; $i <= 12; $i += $cols)
    {
        $yeartable .= "  <tr>n";
        for ($month = $i; $month <= $i + $cols - 1; $month++)
        {
            $yeartable .= "    <td>n" . genMonth($year,$month,"      ") . "n    </td>n";
        }
        $yeartable .= "  </tr>n";
    }
    $yeartable .= "</table>n";
    
    return $yeartable;
}

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
	<title>Calendar test</title>
</head>

<body>
<blockquote>
To redisplay the calendar in a different number of columns, enter a dividend of 12 (1, 2, 3, 4, 6, 12) in the field below. 
A value that is not a dividend of 12 will be ignored, and a 3 column calendar shown.
</blockquote>
<p>
<form method="get" action="calendar.php">
    Number of columns?
    <input name="cols" type="text" size="3" value="3">
    <input type="submit" name="submit" value="Redisplay">
</form>

<hr>

<?php echo genYear(2000,$cols); ?>

</body>
</html>