Today's date is: Saturday, May 4, 2024

Tedd's php calendar


May 2024

Sun M T W Th F Sat
   1234
567891011
12131415161718
19202122232425
262728293031

Go To:

Assignment -- create this calendar.

The following are the functions you will need to complete this project.

// get current date
$current_date = getdate();

// get numeric month (i.e., 1-12)
$mon = $current_date["mon"];

// get alpha month (i.e., January)
$month = $current_date["month"];

// get four digit year (i.e., 2012)
$year = $current_date["year"];

// create a date by using the getdate() and mktime() functions
$what_date = getdate(mktime(0, 0, 0, $mon, 1, $year));

//get the month selected
$month = $what_date["month"];

// get the first weekday of that month
$first_week_day= $what_date["wday"];

// get the number of days in that month
// This is done by using the date('t') and mktime() functions and
// increasing the current month by +1
$days_in_month = date('t', mktime(0, 0, 0, $next_month, 0, $year))

Sat May 04 2024, sunrise time : 05:30
Sat May 04 2024, sunset time : 19:37


CODE FOLLOWS

<?php

    error_reporting
(E_ALL);    // set error reporting to all 
    
$self basename($_SERVER['SCRIPT_NAME']);

    
// current date
    
$current_date getdate();

    
$mon $current_date["mon"];            // numeric month (1-12)
    
$month $current_date["month"];        // display month (i.e., January)
    
$year $current_date["year"];            // four digit year (i.e., 2012)

    // get the user's selection (if any)    of use default    
    
$mon = isset($_POST['mon']) ? $_POST['mon'] : $mon;
    
$year = isset($_POST['year']) ? $_POST['year'] : $year;

    
// based upon above, what month and year has been sellected?
    
$what_date getdate(mktime(000$mon1$year)); 

    
//get the name of the month selected
    
$month $what_date["month"]; 

    
// get the first weekday of that month
    
$first_week_day$what_date["wday"];

    
// -----------------
    
    // NOTE: The following are four different solutions to 
    // determine the number of days in a month
        
    // [1] By using the getdate() and mktime() functions together 
    // and setting the month day to 32 and substracting any difference
        
    
$what_date getdate(mktime(000$mon32$year));
    
$days_in_month 32 $what_date['mday'];

    
// -----------------
    
    // [2] By using the getdate() and mktime() functions and
    // increasing the current month by +1 and setting the day to 0 -- that
    // will be the first day of the next month == last day of current month
        
    
$next_month $mon 1;
    
$what_date getdate(mktime(000$next_month0$year));
    
$days_in_month $what_date['mday'];

    
// -----------------
        
    // [3] Or by using a function
    
    
$days_in_month numberDaysMonth($mon$year);

    
// -----------------
    
    // [4] Or by using date()

    
$days_in_month date('t'mktime(000$next_month0$year));
                                
     include(
'includes/header.php');
 
?>

        <p class="floatR">
            Today's date is: <?php echo(date("l, M j, Y")); ?>
        </p>
        
        <h1>Tedd's php calendar</h1>
        
        <table class="php">
            <tr>
                <td colspan="7">
                    <table class="full">
                        <tr>
                            <td class="left">
                                <form action="<?php echo($self); ?>" method="post" >  
                                    <input type="hidden" name="mon" value="<?php if (($mon-1)<1) { echo ("12"); }  else { echo $mon-1; } ?>">
                                    <input type="hidden" name="year" value="<?php if (($mon-1)<1) { echo ($year-1); } else { echo $year; } ?>">
                                    <br><input type="submit" value="Prev">
                                </form>
                            </td>
                            <td class="center">
                                <?php echo (" <b>$month $year</b> "); ?>
                            </td>
                            <td class="right">
                                <form action="<?php echo($self); ?>" method="post" >  
                                    <input type="hidden" name="mon" value="<?php if (($mon+1)>12) { echo ("1"); } else { echo $mon+1; } ?>">
                                    <input type="hidden" name="year" value="<?php if (($mon+1)>12) { echo ($year+1); } else { echo $year; } ?>">
                                    <br>
                                    <input type="submit" value="Next">
                                </form>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
            <tr>
                <th class="day-of-week">Sun</th>
                <th class="day-of-week">M</th>
                <th class="day-of-week">T</th>
                <th class="day-of-week">W</th>
                <th class="day-of-week">Th</th>
                <th class="day-of-week">F</th>
                <th class="day-of-week">Sat</th>
            </tr>
            
            <?php 
                
// The following is only to recognize the *current* date 
                // in the calendar presentation
                
                
$day 1;
                
$this_day date('d');
                
$this_mon date('n');
                
$this_month date('F');
                
$this_year date('Y');
                
                
// The following is used for every month
                
                
$week_day $first_week_day;
                
$first_week true;
            
                
// loop through all the days of the month
                
while ( $day <= $days_in_month)
                    {
                    
// set up blank days for first week
                    
if ($first_week)
                        {
                        echo (
"<tr>");
                        for (
$i 0$i $first_week_day$i++)
                            {
                            echo (
"<td class=\"non-day\">&nbsp;</td>");
                            }
                        
$first_week false;
                        }
    
                    if (
$week_day == 0)    // Sunday -- start week with <tr>
                        
{
                        echo (
"<tr>");
                        }
                        
                    if ((
$day == $this_day) && ($mon == $this_mon) && ($year == $this_year))
                        {
                        echo (
"<td class= \"current\">$day</td>");
                        }
                    else
                        {
                        echo (
"<td class=\"day\">$day</td>");
                        }
                    
                    if (
$week_day == 6)    // Saturday -- end week with </tr>
                        
{
                        echo (
"</tr>");
                        }
                    
                    
$week_day++;
                    
$week_day $week_day 7;
                    
$day++;
                    }
                
?>
                
            <tr >
                <td colspan="7">
                    <form action="<?php echo($self); ?>" method="post" >  
                        <br>
                        Go To:

                        <label for="mon">Month: </label>
                        <select id="mon" name="mon">
                            <option value="<?php echo ($this_mon); ?>">
                            <?php echo ($this_month); ?></option>
                            <option value="<?php echo ($this_mon); ?>">----------</option>
                            <option value="1">January</option>
                            <option value="2">February</option>
                            <option value="3">March</option>
                            <option value="4">April</option>
                            <option value="5">May</option>
                            <option value="6">June</option>
                            <option value="7">July</option>
                            <option value="8">August</option>
                            <option value="9">September</option>
                            <option value="10">October</option>
                            <option value="11">November</option>
                            <option value="12">December</option>
                        </select>
                        <label for="year">Year: </label>
                        <select id="year" name="year">
                            <option value="<?php echo ($this_year); ?>"><?php echo ($this_year); ?></option>
                            <option value="<?php echo ($this_year); ?>">----------</option> 
                            <option value="<?php echo ($this_year -1); ?>"><?php echo ($this_year -1); ?></option>
                            <option value="<?php echo ($this_year); ?>"><?php echo ($this_year); ?></option>    
                            <option value="<?php echo ($this_year 1); ?>"><?php echo ($this_year 1); ?></option>                        
                            <option value="<?php echo ($this_year 2); ?>"><?php echo ($this_year 2); ?></option>                        
                        </select> 
                        <input type="submit" name="submit" value="GoTo">
                    </form>
                </td>
            </tr>
        </table>
        <br>
        <h2>
            Assignment -- create this calendar.
        </h2>
        <h3>
            The following are the functions you will need to complete this project.
        </h3>
        
        <p>
            // get current date <br>
            $current_date = getdate(); <br><br>
            
            // get numeric month (i.e., 1-12) <br>
            $mon = $current_date["mon"];<br><br>
            
            // get alpha month (i.e., January) <br>
            $month = $current_date["month"];<br><br>
            
            // get four digit year (i.e., 2012)<br>    
            $year = $current_date["year"];<br><br>    
            
            // create a date by using the getdate() and mktime() functions<br>
            $what_date = getdate(mktime(0, 0, 0, $mon, 1, $year)); <br><br>
        
            //get the month selected<br>
            $month = $what_date["month"]; <br><br>
        
            // get the first weekday of that month<br>
            $first_week_day= $what_date["wday"];<br><br>
            
            // get the number of days in that month<br>
            // This is done by using the date('t') and mktime() functions and<br>
            // increasing the current month by +1<br>
            $days_in_month = date('t', mktime(0, 0, 0, $next_month, 0, $year))
        </p>

<?php

    
echo date("D M d Y"). ', sunrise time : ' 
        
date_sunrise(time(), SUNFUNCS_RET_STRING42.57, -84.332090, -5);
    echo(
'<br>');
    echo 
date("D M d Y"). ', sunset time : ' 
        
date_sunset(time(), SUNFUNCS_RET_STRING42.57, -84.332090, -5);
    echo(
'<br>');
    echo(
'<br>');
        
    include(
'includes/footer.php'); 

//    ======== functions ===========

function numberDaysMonth($month$year)
    {
    
// Leap year is definded as a year that is evenly divisible by four
    // AND (year NOT evenly divisible by 100 OR year IS evenly divisible by 400) 
    
    
$feb = ($year%== && ($year%100 != || $year%400 == 0) ) ? 29 28;
    
$days = array(031$feb31303130313130313031);
    return 
$days[$month];
    }
    
?>

Last modified: January 13 2022