Home >> Hobbies >> PHP Programming

Hobbies --> PHP Programming

2-Week Calendar Creator

I needed to create a 2-week calendar to log activities at work.  I could have created this manually, but thought it would be fun to write a PHP script to do the job for me. 

Below is the source code for the script.  I didn't have a chance to comment this as much as I would have liked.  Also, you could break this code up so that the lines aren't so long, and the script would work the same.


<?php

<html>
<head>
<title>Calendar</title>
<STYLE TYPE="text/css">P.breakhere {page-break-before: always}</STYLE>
<style type="text/css">
<!--
body            {font-family: arial, helvetica, geneva, sans-serif; font-size: 9pt; color: #000000; background-color: #FFFFFF}
pre, tt         {font-size: 9pt}
th              {font-family: arial, helvetica, geneva, sans-serif; font-size: 9pt; font-weight: bold; color: #000000}
td              {font-family: arial, helvetica, geneva, sans-serif; font-size: 9pt}
h1              {font-family: arial, helvetica, geneva, sans-serif; font-size: 16pt; font-weight: bold}
A:link          {font-family: arial, helvetica, geneva, sans-serif; font-size: 9pt; text-decoration: none; color: #0000FF}
A:visited       {font-family: arial, helvetica, geneva, sans-serif; font-size: 9pt; text-decoration: none; color: #0000FF}
A:hover         {font-family: arial, helvetica, geneva, sans-serif; font-size: 9pt; text-decoration: underline; color: #FF0000}
A.nav:link      {font-family: arial, helvetica, geneva, sans-serif; color: #000000}
A.nav:visited   {font-family: arial, helvetica, geneva, sans-serif; color: #000000}
A.nav:hover     {font-family: arial, helvetica, geneva, sans-serif; color: #FF0000}
.nav            {font-family: arial, helvetica, geneva, sans-serif; color: #000000}
.warning        {font-family: arial, helvetica, geneva, sans-serif; font-size: 9pt; font-weight: bold; color: #FF0000}
//-->
</style>
</head>
<body bgcolor="white">
<?php
if ($_POST['run'])  // this is the code that will be executed if the form has been submitted.
{
$date = array();
$startdate = $_POST['startdate'];
$pages = $_POST['pages'];
$fontcolor = $_POST['fontcolor'];
$weekendcolor = $_POST['weekendcolor'];
$linebreaks = $_POST['linebreaks'];
$endtime = $pages * 14;
$pieces = explode("/",$startdate);
$month = $pieces[0];
$day = $pieces[1];
$year = $pieces[2];
$unixtime = mktime(0,0,0,$month,$day,$year);
// calculate the dates
for ($a=1; $a<=$endtime; $a++)
 { $date[$a] = date("D, M j", $unixtime); 
   $unixtime += 86400;
 }
// set up the line breaks
for ($a=1; $a<=$linebreaks; $a++)
{ $spacer = $spacer . "<br>"; }
echo "<center>\n";
for ($b=0; $b<$pages; $b++)
{
echo "<table>\n";
echo "<tr><td>\n";
// Week 1
echo "<table border=\"1\" width=\"450\">\n";
$index = $b*7*2 + 1 ;
echo "<tr bgcolor=\"$weekendcolor\"><td width=\"100\" align=\"center\" height=\"1\"><font color=\"$fontcolor\"><b>$date[$index]</b></font></td><td rowspan=\"2\">&nbsp;</td></tr>\n";
echo "<tr bgcolor=\"$weekendcolor\"><td>&nbsp;$spacer<br></td>\n";
$index = $b*7*2 + 2;
echo "<tr><td width=\"100\" align=\"center\" height=\"1\"><font color=\"$fontcolor\"><b>$date[$index]</b></font></td><td rowspan=\"2\">&nbsp;</td></tr>\n";
echo "<tr><td>&nbsp;$spacer<br></td>\n";
$index = $b*7*2 + 3;
echo "<tr><td width=\"100\" align=\"center\" height=\"1\"><font color=\"$fontcolor\"><b>$date[$index]</b></font></td><td rowspan=\"2\">&nbsp;</td></tr>\n";
echo "<tr><td>&nbsp;$spacer<br></td>\n";
$index = $b*7*2 + 4;
echo "<tr><td width=\"100\" align=\"center\" height=\"1\"><font color=\"$fontcolor\"><b>$date[$index]</b></font></td><td rowspan=\"2\">&nbsp;</td></tr>\n";
echo "<tr><td>&nbsp;$spacer<br></td>\n";
$index = $b*7*2 + 5;
echo "<tr><td width=\"100\" align=\"center\" height=\"1\"><font color=\"$fontcolor\"><b>$date[$index]</b></font></td><td rowspan=\"2\">&nbsp;</td></tr>\n";
echo "<tr><td>&nbsp;$spacer<br></td>\n";
$index = $b*7*2 + 6;
echo "<tr><td width=\"100\" align=\"center\" height=\"1\"><font color=\"$fontcolor\"><b>$date[$index]</b></font></td><td rowspan=\"2\">&nbsp;</td></tr>\n";
echo "<tr><td>&nbsp;$spacer<br></td>\n";
$index = $b*7*2 + 7;
echo "<tr bgcolor=\"$weekendcolor\"><td width=\"100\" align=\"center\" height=\"1\"><font color=\"$fontcolor\"><b>$date[$index]</b></font></td><td rowspan=\"2\">&nbsp;</td></tr>\n";
echo "<tr bgcolor=\"$weekendcolor\"><td>&nbsp;$spacer<br></td>\n";
echo "</table>\n";
echo "</td><td width=\"25\">&nbsp;</td><td>\n";
// Week 2
echo "<table border=\"1\" width=\"450\">\n";
$index = $b*7*2 + 8;
echo "<tr bgcolor=\"$weekendcolor\"><td width=\"100\" align=\"center\" height=\"1\"><font color=\"$fontcolor\"><b>$date[$index]</b></font></td><td rowspan=\"2\">&nbsp;</td></tr>\n";
echo "<tr bgcolor=\"$weekendcolor\"><td>&nbsp;$spacer<br></td>\n";
$index = $b*7*2 + 9;
echo "<tr><td width=\"100\" align=\"center\" height=\"1\"><font color=\"$fontcolor\"><b>$date[$index]</b></font></td><td rowspan=\"2\">&nbsp;</td></tr>\n";
echo "<tr><td>&nbsp;$spacer<br></td>\n";
$index = $b*7*2 + 10;
echo "<tr><td width=\"100\" align=\"center\" height=\"1\"><font color=\"$fontcolor\"><b>$date[$index]</b></font></td><td rowspan=\"2\">&nbsp;</td></tr>\n";
echo "<tr><td>&nbsp;$spacer<br></td>\n";
$index = $b*7*2 + 11;
echo "<tr><td width=\"100\" align=\"center\" height=\"1\"><font color=\"$fontcolor\"><b>$date[$index]</b></font></td><td rowspan=\"2\">&nbsp;</td></tr>\n";
echo "<tr><td>&nbsp;$spacer<br></td>\n";
$index = $b*7*2 + 12;
echo "<tr><td width=\"100\" align=\"center\" height=\"1\"><font color=\"$fontcolor\"><b>$date[$index]</b></font></td><td rowspan=\"2\">&nbsp;</td></tr>\n";
echo "<tr><td>&nbsp;$spacer<br></td>\n";
$index = $b*7*2 + 13;
echo "<tr><td width=\"100\" align=\"center\" height=\"1\"><font color=\"$fontcolor\"><b>$date[$index]</b></font></td><td rowspan=\"2\">&nbsp;</td></tr>\n";
echo "<tr><td>&nbsp;$spacer<br></td>\n";
$index = $b*7*2 + 14;
echo "<tr bgcolor=\"$weekendcolor\"><td width=\"100\" align=\"center\" height=\"1\"><font color=\"$fontcolor\"><b>$date[$index]</b></font></td><td rowspan=\"2\">&nbsp;</td></tr>\n";
echo "<tr bgcolor=\"$weekendcolor\"><td>&nbsp;$spacer<br></td>\n";
echo "</table>\n";
echo "</td></tr>\n";
echo "</table>\n";
if (($b + 1) != $pages) { echo "<P CLASS=\"breakhere\">\n"; }
}
echo "</center>\n";
}  // end of if - This is where the code stops if the form has been submitted.
else // The following code will be displayed if the form hasn't been submitted.  This will create a normal web page to allow the user to input options.
{
?>
<table>
<tr>
<td valign="top">
<form method="POST" action="calendar-creator-live.php">
<input type="hidden" name="run" value="1">
<h3>2-Week Calendar Creator</h3>
<p>Use the form below to create a customized 2-week-per-page calendar. This will create a
two-column calendar that must br printed in landscape mode.</p>
<p>Enter the requested information below, then click the "Create Calendar" button when finished.</p>
Start date for the calendar (mm/dd/yyyy): <input type="text" size="12" name="startdate" value="<?php echo date("m/d/Y"); ?>"><br><br>
How many calendar pages do you want to print? <input type="text" size="3" name="pages" value="1"><br><br>
How many line breaks per day? <select size="1" name="linebreaks">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5" selected>5 - Best Choice</option>
</select><br><br>
Font Color: <select size="1" name="fontcolor">
<option value="#000000">Black</option>
<option value="#ff0000" selected>Red</option>
<option value="#000099">Blue</option>
<option value="#003300">Green</option>
<option value="#660066">Purple</option>
<option value="#ff6600">Orange</option>
</select>&nbsp;&nbsp;
Weekend Color: <select size="1" name="weekendcolor">
<option value="#cccccc">Grey</option>
<option value="#ffffff">White</option>
<option value="#ffffcc">Light Yellow</option>
<option value="#ccffff">Light Blue</option>
<option value="#ffcccc">Pink</option>
<option value="#ccccff">Lavender</option>
<option value="#ccffcc">Light Green</option>
<option value="#ffcc99">Light Orange</option>
</select><br><br>
<p><b>Notes:</b></p>
<li>Each page includes a 2-week calendar.
<li> In order to print background colors, for Internet Explorer,
under "Internet Options", select the "Advanced" tab. In the window of options, locate the "Printing"
section. Check the box entitled "Print background colors and image".
<li> Be sure to set your printer to landscape mode. 
<li> Set your page margins to 0.25" all around for a well-spaced page.<br><br>
<input type="submit" value="Create Calendar" name="Submit">
</form>
</td>
<td valign="top">
<h3>Calendar Example</h3>
<img src="images/calendar.gif">
</td>
</tr>
</table>
<?php
}
?>
</body>
</html>

?>

 


Home >> Hobbies >> PHP Programming

webmaster at vansvault dot org

Last modified: February 20, 2006