Thursday, 4 June 2015

PHP: Function to generate calendars on the fly

<?php 

function makecal($stmonth, $styear) {

    ## define variables-little cleaner
    ## than mucking over the code to
    ## change stuff
    ##
    
    $face           = "verdana";  ## font face for all text
    $size           = "1";        ## font size for all text
    $height         = "22";       ## cell height
    $width          = "22";       ## cell width
    $todaycolor     = "#CC0000";  ## font color for the current date
    $inmonthface    = "#000000";  ## font color for days in the display month    
    $outmonthface   = "#666666";  ## font color for days not in the display month
    $inbgcolor      = "#FFFFCC";  ## cell bgcolor for days in the display month
    $outbgcolor     = "#FFCCFF";  ## cell bgcolor for days not in display month


    ## make workie code stuph
    ##
    
    $stdate = mktime(0,0,0,$stmonth,1,$styear);
    $startdate = $currdate = mktime(0,0,0,$stmonth,1 - date("w", mktime(0,0,0,$stmonth,1,$styear)),$styear);
    $enddate = mktime(0,0,0,date("m",$stdate) + 1,7 - date("w", mktime(0,0,0,$stmonth + 1,0,$styear)),$styear);
    
    echo "<table border=1 cellpadding=1 cellspacing=0>\n";
    echo "<tr>\n<th colspan=7 height=\"$height\"><font face=\"$face\" size=\"$size\">" . date("F",$stdate) . " " . date
("Y",$stdate) . "</th>\n</tr>\n";
    echo "<tr>\n<th width=\"$width\" height=\"$height\" valign=\"middle\" align=\"center\"><font face=\"$face\"
size=\"$size\">Su</th>\n";
    echo "<th width=\"$width\" height=\"$height\" valign=\"middle\" align=\"center\"><font face=\"$face\"
size=\"$size\">Mo</th>\n";
    echo "<th width=\"$width\" height=\"$height\" valign=\"middle\" align=\"center\"><font face=\"$face\"
size=\"$size\">Tu</th>\n";
    echo "<th width=\"$width\" height=\"$height\" valign=\"middle\" align=\"center\"><font face=\"$face\"
size=\"$size\">We</th>\n";
    echo "<th width=\"$width\" height=\"$height\" valign=\"middle\" align=\"center\"><font face=\"$face\"
size=\"$size\">Th</th>\n";
    echo "<th width=\"$width\" height=\"$height\" valign=\"middle\" align=\"center\"><font face=\"$face\"
size=\"$size\">Fr</th>\n";
    echo "<th width=\"$width\" height=\"$height\" valign=\"middle\" align=\"center\"><font face=\"$face\"
size=\"$size\">Sa</th>\n</tr>\n";

    $i=0;
    while ($currdate < $enddate) {
        echo "<tr>\n";
        for ($c = 0; $c < 7; $c++) {
            if (date("m",$stdate) == date("m",$currdate)) {
                $bgcolor = $inbgcolor;
            } else {
                $bgcolor = $outbgcolor;
            }
            if (date("d",$currdate) == date("d") && date("m",$currdate) == date("m") && date("Y",$currdate) == date
("Y")) {
                $fcolor = $todaycolor;
            } elseif (date("m",$currdate) == date("m",$stdate) && date("Y",$currdate) == date("Y",$stdate)) {
                $fcolor = $inmonthface;
            } else {
                $fcolor = $outmonthface;
            }
            echo "<td align=\"center\" height=\"$height\" width=\"$width\" valign=\"middle\" bgcolor=\"$bgcolor\"><font
face=\"$face\" size=\"$size\"
color=\"$fcolor\">";
            echo date ("j",$currdate);
            echo "</font></td>\n";
            $i++;
            $currdate = mktime(0,0,0,date("m",$startdate),date("d",$startdate) + $i,date("Y",$startdate));
        }
        echo "</tr>\n";
    }
    echo "</table>";

}
    

## example usage
##

echo "<html><head></head><body bgcolor=\"#ffffff\">\n\n";
echo "<form action=cal.php3 method=post>\n";
echo "<select name=stmonth>\n";
echo "<option>1\n";
echo "<option>2\n";
echo "<option>3\n";
echo "<option>4\n";
echo "<option>5\n";
echo "<option>6\n";
echo "<option>7\n";
echo "<option>8\n";
echo "<option>9\n";
echo "<option>10\n";
echo "<option>11\n";
echo "<option>12\n";
echo "</select>\n";
echo "<select name=styear>\n";
echo "<option>1999\n";
echo "<option>2000\n";
echo "<option>2001\n";
echo "<option>2002\n";
echo "<option>2003\n";
echo "<option>2029\n";
echo "</select>\n";
echo "<input type=submit value=\"show cal\">\n<input type=submit name=source value=\"view source\">\n</form>\n\n";
echo "<BR>\n\n";

if ($stmonth && $styear && empty($source)) {
    echo makecal($stmonth,$styear);
} elseif (empty($source)) {
    echo makecal(date("m"),date("Y"));
} else {
    echo "<code><font size=1>\n";
    show_source("cal.php3");
    echo "</font></code>\n";
}

echo "</body>\n</html>";

?>

0 comments:

Post a Comment