Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 1 of 1
06-13-2013, 02:46 AM #1
- Join Date
- Sep 2010
- Thanked 226 Times in 226 Posts
Build a monthly HTML calendar with php and the bash shell cal function.
This is a script that takes a monthly calendar produced by the Unix cal function, part of the BASH shell and found on all 'nix machines and Apples, and uses php to convert it into an HTML table calendar. The first part is the form, which I have on a separate index page. Then the part that goes at the top of the builder page. Then part that can go just about anywhere on the builder page. The texarea is there only for informational puposes and can be left off an 'operational' build.
<fieldset><form action="./builder.php" method="get">
<input type="hidden" name="mydata" id="mydata">
If you want a calender for this month, put nothing here: <input type="text" name="m-y" size=10 /><br>
<input type="submit" value="Load calendar"><br>
If you want a calender for a specific month in the past or future, for example July 1977, enter it this way: 7 1977, or 07 1977, just two numbers with a space.<br><br> To make a calender for this year just put in: -y.<br><br>To make a calender for a specific year, just put in the year: 1996, with no -y.<br><br>
Now the top of the builder.
$mnyr = $_GET['m-y'];
$cal = `cal $mnyr`;
$cal = `echo '$cal' | sed '/^$/d' `;
$cal = trim($cal);
The part that actually builds the calendar.
<textarea id="showcal" cols=21 rows=10 >
$cal_string = str_replace("\n","\n ",$cal);
$weeks = explode("\n",$cal_string);
$first_day = (strpos($weeks,"1")+1)/3;
$month = trim($weeks);
$find_last = trim($cal);
$size = strlen($find_last)-2;
$last_day = substr($find_last,$size);
echo "<table id=\"mytable\"><tr><caption class=\"Month\">".$month."</caption></tr><tr>\n";
echo "<th class=\"day\">Sunday</th>\n";
echo "<th class=\"day\">Monday</th>\n";
echo "<th class=\"day\">Tuesday</th>\n";
echo "<th class=\"day\">Wednesday</th>\n";
echo "<th class=\"day\">Thursday</th>\n";
echo "<th class=\"day\">Friday</th>\n";
echo "<th class=\"day\">Saturday</th>\n</tr><tr>\n";
$stack = count($weeks)-2;
for ( $i = 1 ; $i <= $stack*7 ; $i++ )
if ( $i < $first_day ) echo "<td class=\"dom\"></td>\n";
elseif ( $i > $first_day-1 && $i < $last_day+$first_day ) echo "<td class=\"dom\">".($i-$first_day+1 )."</td>\n";
else echo "<td class=\"dom\"></td>\n";
if ( $i%7 == 0 && $i < $stack*7 ) echo "</tr><tr>\n";
You may be able to omit the part with Sed.
Last edited by DrDOS; 06-13-2013 at 02:49 AM.Welcome to http://www.myphotowizard.net
where you can edit images, make a photo calendar, add text to images, and do much more.
When you know what you're doing it's called Engineering, when you don't know, it's called Research and Development. And you can always charge more for Research and Development.