![calendar](https://truelogic.org/wordpress/wp-content/uploads/2012/06/calendar.png)
![calendar](http://truelogic.org/wordpress/wp-content/uploads/2012/06/calendar.png)
There are lots of php based calendar scripts out there, so this one is nothing new. This is just my contribution to the list out there. This shows a calendar in the month format. The months can be changed by clicking on the month hyperlink in the page. It assumes that the year is always the current year. This can be easily changed by passing it as an argument.
Most first-timers get stuck in figuring out how to find the first weekday of the month and how to find the last day/weekday of the month. This has been clearly shown in the code. The process flow is simple:
- Check for the month and year requested. Here the year is just taken as the current year. Month is passed as a querystring. If none is present, it takes the current month
- Calculate starting weekday, ending weekday, no.of days in the month
- Create a double dim array corresponding to the UI grid of the calendar table. 6 rows of 7 columns each.
- As per the current month specs, fill in the required cells of the array and leave the rest blank
- Once the array is initialized, run a loop through it to show the output in a grid.
- Each date cell has been checked for a click event in javascript , using simple jQuery.
The php initialization code snippet is given below:
// get params $monthArg = $_GET['m']; // calc dates and weekdays if ($monthArg == null || $monthArg == '') $currMonth= date("m"); else $currMonth = intval($monthArg); $currYear = date("Y"); $startDate = strtotime($currYear . "-" . $currMonth . "-01 00:00:01"); $startDay= date("N", $startDate); $monthName = date("M",$startDate ); //echo("start day=". $startDay . "<br>"); $daysInMonth = cal_days_in_month(CAL_GREGORIAN, date("m", $startDate), date( "Y", $startDate)); $endDate = strtotime($currYear . "-" . $currMonth . "-" . $daysInMonth ." 00:00:01"); //echo(date("Y", $endDate) . "-" . date("m", $endDate) . "-". date("d", $endDate)); $endDay = date("N", $endDate); //echo("end day=" . $endDay . "<br>"); // initialize structure array matching the calendar grid // 6 rows and 7 columns // php date sunday is zero if ($startDay> 6) $startDay = 7 -$startDay; $currElem = 0; $dayCounter = 0; $firstDayHasCome = false; $arrCal = array(); for($i = 0; $i <= 5; $i ++) { for($j= 0; $j <= 6; $j++) { // decide what to show in the cell if($currElem < $startDay && !$firstDayHasCome) $arrCal[$i][$j] = ""; else if ($currElem == $startDay && !$firstDayHasCome) { $firstDayHasCome= true; $arrCal[$i][$j] = ++$dayCounter; } else if ($firstDayHasCome) { if ($dayCounter < $daysInMonth) $arrCal[$i][$j] = ++ $dayCounter; else $arrCal[$i][$j] = ""; } $currElem ++; } } ?>
The code is not exactly the most efficient or the best, but its a good example of generating a calendar. Any comments and critiques are welcome. You are free to use the code or modify it any way you want for your own purposes.
This is great! Thanks so much for this. One issue I noticed is that empty weeks show up in certain months. See October 2012. Any idea how to fix this?
Thanks!
Thanks Casey. The last row that shows up is for certain cases where 6 rows are required eg .Dec 2012. I guess there can be a check in the loop that if the last day has been displayed then exit the loop and stop display of further columns.
How can you change this to enable to display years? Instead of just the one year.
@Vic An easy way would be to show the year as a dropdown in HTML perhaps or show a hyperlink for Next Year and Prev Year.
The line $currYear = date(“Y”); would then become $currYear = $_GET[“y”]