Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 1 of 1
  1. #1
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,915
    Thanks
    15
    Thanked 227 Times in 227 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.
    PHP Code:
    <div id="uploadform">
    <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:&nbsp;<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>
    </form></fieldset>
    </div>

    Now the top of the builder.

    <?php 
    @session_start();
    if (isset(
    $_GET['mydata'])){
        
    $mnyr $_GET['m-y'];
        
    $cal = `cal $mnyr`;
        
    $cal = `echo '$cal' | sed '/^$/d' `; 
        
    $cal trim($cal);
        }
    ?>

    The part that actually builds the calendar.

    <div id="wrapper">
    <textarea id="showcal" cols=21 rows=10 >
    <?php
    $cal_string 
    str_replace("\n","\n ",$cal);
    echo 
    $cal_string;
    ?>
    </textarea>
    <?php
    $weeks 
    explode("\n",$cal_string);
    $first_day = (strpos($weeks[2],"1")+1)/3;
    //echo $first_day;
    $month trim($weeks[0]);
    $find_last trim($cal);
    $size strlen($find_last)-2;
    $last_day substr($find_last,$size);
    //echo count($weeks);
    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 $i <= $stack*$i++ )
        {
    if ( 
    $i $first_day ) echo "<td class=\"dom\"></td>\n";
    elseif ( 
    $i $first_day-&& $i $last_day+$first_day ) echo "<td class=\"dom\">".($i-$first_day+)."</td>\n";
    else  echo 
    "<td class=\"dom\"></td>\n";
    if  ( 
    $i%== && $i $stack*) echo "</tr><tr>\n";
        }
    echo 
    "</tr></table>\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.


 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •