PHP Basic Calendar Tutorial By UnknownGhost03 Fix!
Highlights is the trick to start the week with Monday instead of Sunday.
Source code:
<html> <head> <title>PHP Basic Calendar</title> <script> function goLastMonth(month, year){ if(month == 1) { --year; month = 13; } --month var monthstring = ""+month+""; var monthlength = monthstring.length; if(monthlength <=1){ monthstring = "0" + monthstring; } document.location.href = "<?php $_SERVER['PHP_SELF'];?>?month="+monthstring+"&year="+year; } function goNextMonth(month, year){ if(month == 12) { ++year; month = 0; } ++month var monthstring = ""+month+""; var monthlength = monthstring.length; if(monthlength <=1){ monthstring = "0" + monthstring; } document.location.href = "<?php $_SERVER['PHP_SELF'];?>?month="+monthstring+"&year="+year; } </script> </head> <body> <?php //get current date or specific month and year if (isset($_GET['day'])){ $day = $_GET['day']; } else { $day = date("d"); } if(isset($_GET['month'])){ $month = $_GET['month']; } else { $month = date("m"); } if(isset($_GET['year'])){ $year = $_GET['year']; }else{ $year = date("Y"); } //get date data for display such as month name $currentTimeStamp = strtotime( "01-$month-$year"); $monthName = date("F", $currentTimeStamp); ?> <table border="1px" cellpadding="0px" cellspacing="0px"> <tr> <td align="center"> <input style='width:50px;' type='button' value='<'name='previousbutton' onclick ="goLastMonth(<?php echo $month.','.$year?>)"> </td> <td align="center" colspan="5"><?php echo $monthName." ".$year; ?></td> <td align="center"> <input style='width:50px;' type='button' value='>'name='nextbutton' onclick ="goNextMonth(<?php echo $month.','.$year?>)"> </td> </tr> <tr> <td align="center" width='100px'>Monday</td> <td align="center" width='100px'>Tuesday</td> <td align="center" width='100px'>Wednesday</td> <td align="center" width='100px'>Thursday</td> <td align="center" width='100px'>Friday</td> <td align="center" width='100px'>Saturday</td> <td align="center" width='100px'>Sunday</td> </tr> <tr> <?php //get the number of days in the current month and year $numDays = date("t", $currentTimeStamp); //keep track of the number of cell created $counter = 0; for($i = 1; $i < $numDays+1; $i++, $counter++){ $timeStamp = strtotime("$year-$month-$i"); //create empty cell until first day of the month if($i == 1) { //0=sun, 1=mon, 2=tue, ... $firstDay = date("w", $timeStamp); //if sunday change the firstDay to 7 if($firstDay == 0) $firstDay = 7; //decrement firstDay by 1 $firstDay--; for($j = 0; $j < $firstDay; $j++, $counter++) { echo "<td> </td>"; } } //create new row if($counter % 7 == 0) { echo"</tr><tr>"; } //print day number echo "<td>$i</td>"; } //fill up the leftover cells of the table while($counter % 7 != 0) { echo "<td> </td>"; $counter++; } ?> </tr> </table> </body> </html>