Search code examples
phpcalendar

How do I implement previous next month link in the calendar


I am creating the below-attached diagram way calendar to capture the activity for each day. enter image description here

Showing the month of a full day, date, and each row will be tracked.

Below is my calendar code so far,

<?php

define("ADAY", (60 * 60 * 24));

if (isset($_POST['month']) && isset($_POST['year'])) {
    $month = $_POST['month'];
    $year = $_POST['year'];
} else {
    $nowArray = getdate();
    $month = $nowArray['mon'];
    $year = $nowArray['year'];
}

$start = mktime(12, 0, 0, $month, 1, $year);
$firstDayArray = getdate($start);
$last_day = cal_days_in_month(CAL_GREGORIAN, $month, $year);
$monthname = date("F", mktime(0, 0, 0, $month, 1, $year));

$pre = date('F', strtotime('-1 month', strtotime($monthname)));
$nex = date('F', strtotime('+1 month', strtotime($monthname)));
?>
<html>
<head><title>Test Calendar</title><head>
<body>
<form method="post" action="<?php print "$_SERVER[PHP_SELF]";?>">
selected <?php echo $monthname;
echo $year; ?> <br>
<a class="col-xs-12" href="#" id="prev">Prev</a>

<select name="month" id="month_val"><?php
$months = array("January", "February", "March", "April", "May",
    "June", "July", "August", "September", "October", "November", "December");
for ($x = 1; $x <= count($months); $x++) {
    print "\t<option value=\"$x\"";
    print($x == $month) ? " SELECTED" : "";
    print ">" . $months[$x - 1] . "\n";
}
?>
</select>
<select name="year"><?php
for ($x = 1980; $x <= $year; $x++) {
    print "\t<option";
    print($x == $year) ? " SELECTED" : "";
    print ">$x\n";
}
?></select>
<input type="submit" value="Go!">
<input type="hidden" id="month_name" value="<?php $monthname?>">
<a class="col-xs-12 aright" id="next" href="#">Next </a><br>
</form>
<?php
$days = array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");

print "<TABLE BORDER = 1 CELLPADDING=5>\n";
print "<tr style='background-color:grey;'><td colspan=3>          Day      </td>      <td colspan=2>          Activity 1      </td>      <td colspan=2>          Activity 2      </td>  </tr>";
for ($i = 1; $i <= $last_day; $i++) {
    $dayname = date("l", mktime(0, 0, 0, $month, $i, $year));
    print "<tr><td colspan='3'>$dayname $i $monthname $year</td> <td colspan='2' bgcolor=''> Activity 1 </td><td colspan='2' bgcolor=''>Activity 2</td></tr>";
}

?>
</body>
</html>

My question: How do I implement the previous, and next links to go a corresponding month and year?

thanks


Solution

  • I think that the overall could be optimized a bit, anyway something like that should work:

    <?php
    
    // calculate previous and next months and years
    
    $prevStamp = strtotime("first day of previous month", $start);
    $prevMonth = date('m', $prevStamp);
    $prevYear = date('Y', $prevStamp);
    
    $nextStamp = strtotime("first day of next month", $start);
    $nextMonth = date('m', $nextStamp);
    $nextYear = date('Y', $nextStamp);
    ?>
    

    Then you have a bunch of options to activate prev and next links:

    • The simplest is switching to get method in the form and then read the variables from $_GET, in this way you can create two tags like:
    <a href="?month=<?= $prevMonth ?>&year=<?= $prevYear ?>" >Prev</a>
    <a href="?month=<?= $nextMonth ?>&year=<?= $nextYear ?>" >Next</a>
    

    • Another option is to let the form action as it is and use the same tags of the previous option but reading the $_REQUEST variable instead of $_GET or $_POST

    • A third option can be removing the prev and next tags from the form and create a specific form for each of them, so you can read the $_POST variable in any case
    <form method="post">
       <input type="hidden" name="month" value="<?= $prevMonth ?>">
       <input type="hidden" name="year" value="<?= $prevYear ?>">
       <a href="#" onclick="this.parentNode.submit()">Prev</a>
    </form
    
    <form method="post">
       <input type="hidden" name="month" value="<?= $nextMonth ?>">
       <input type="hidden" name="year" value="<?= $nextYear ?>">
       <a href="#" onclick="this.parentNode.submit()">Next</a>
    </form