Skip to content

Instantly share code, notes, and snippets.

@andiaziz
Forked from joecue/calendar.php
Created November 14, 2021 19:12
Show Gist options
  • Save andiaziz/75e8decd4ea21142ab7cb29e66980aaf to your computer and use it in GitHub Desktop.
Save andiaziz/75e8decd4ea21142ab7cb29e66980aaf to your computer and use it in GitHub Desktop.
PHP Script to Generate a Calendar Grid for the month of the Current date.
<html>
<head>
<link href='https://fonts.googleapis.com/css?family=Lato' rel='stylesheet' type='text/css'>
<style>
body{font-family: Lato;}
caption{font-size: 22pt; margin: 10px 0 20px 0; font-weight: 700;}
table.calendar{width:100%; border:1px solid #000;}
td.day{width: 14%; height: 140px; border: 1px solid #000; vertical-align: top;}
td.day span.day-date{font-size: 14pt; font-weight: 700;}
th.header{background-color: #003972; color: #fff; font-size: 14pt; padding: 5px;}
.not-month{background-color: #a6c3df;}
td.today {background-color:#efefef;}
td.day span.today-date{font-size: 16pt;}
</style>
</head>
</html>
<?php
function build_calendar($month,$year,$dateArray) {
// Create array containing abbreviations of days of week.
$daysOfWeek = array('Sun','Mon','Tues','Wed','Thurs','Fri','Sat');
// What is the first day of the month in question?
$firstDayOfMonth = mktime(0,0,0,$month,1,$year);
// How many days does this month contain?
$numberDays = date('t',$firstDayOfMonth);
// Retrieve some information about the first day of the
// month in question.
$dateComponents = getdate($firstDayOfMonth);
// What is the name of the month in question?
$monthName = $dateComponents['month'];
// What is the index value (0-6) of the first day of the
// month in question.
$dayOfWeek = $dateComponents['wday'];
// Create the table tag opener and day headers
$calendar = "<table class='calendar'>";
$calendar .= "<caption>$monthName $year</caption>";
$calendar .= "<tr>";
// Create the calendar headers
foreach($daysOfWeek as $day) {
$calendar .= "<th class='header'>$day</th>";
}
// Create the rest of the calendar
// Initiate the day counter, starting with the 1st.
$currentDay = 1;
$calendar .= "</tr><tr>";
// The variable $dayOfWeek is used to
// ensure that the calendar
// display consists of exactly 7 columns.
if ($dayOfWeek > 0) {
$calendar .= "<td colspan='$dayOfWeek' class='not-month'>&nbsp;</td>";
}
$month = str_pad($month, 2, "0", STR_PAD_LEFT);
while ($currentDay <= $numberDays) {
// Seventh column (Saturday) reached. Start a new row.
if ($dayOfWeek == 7) {
$dayOfWeek = 0;
$calendar .= "</tr><tr>";
}
$currentDayRel = str_pad($currentDay, 2, "0", STR_PAD_LEFT);
$date = "$year-$month-$currentDayRel";
if ($date == date("Y-m-d")){
$calendar .= "<td class='day today' rel='$date'><span class='today-date'>$currentDay</span></td>";
}
else{
$calendar .= "<td class='day' rel='$date'><span class='day-date'>$currentDay</span></td>";
}
// Increment counters
$currentDay++;
$dayOfWeek++;
}
// Complete the row of the last week in month, if necessary
if ($dayOfWeek != 7) {
$remainingDays = 7 - $dayOfWeek;
$calendar .= "<td colspan='$remainingDays' class='not-month'>&nbsp;</td>";
}
$calendar .= "</tr>";
$calendar .= "</table>";
return $calendar;
}
function build_previousMonth($month,$year,$monthString){
$prevMonth = $month - 1;
if ($prevMonth == 0) {
$prevMonth = 12;
}
if ($prevMonth == 12){
$prevYear = $year - 1;
} else {
$prevYear = $year;
}
$dateObj = DateTime::createFromFormat('!m', $prevMonth);
$monthName = $dateObj->format('F');
return "<div style='width: 33%; display:inline-block;'><a href='?m=" . $prevMonth . "&y=". $prevYear ."'><- " . $monthName . "</a></div>";
}
function build_nextMonth($month,$year,$monthString){
$nextMonth = $month + 1;
if ($nextMonth == 13) {
$nextMonth = 1;
}
if ($nextMonth == 1){
$nextYear = $year + 1;
} else {
$nextYear = $year;
}
$dateObj = DateTime::createFromFormat('!m', $nextMonth);
$monthName = $dateObj->format('F');
return "<div style='width: 33%; display:inline-block;'>&nbsp;</div><div style='width: 33%; display:inline-block; text-align:right;'><a href='?m=" . $nextMonth . "&y=". $nextYear ."'>" . $monthName . " -></a></div>";
}
?>
<?php
parse_str($_SERVER['QUERY_STRING']);
if ($m == ""){
$dateComponents = getdate();
$month = $dateComponents['mon'];
$year = $dateComponents['year'];
} else {
$month = $m;
$year = $y;
}
echo build_previousMonth($month, $year, $monthString);
echo build_nextMonth($month,$year,$monthString);
echo build_calendar($month,$year,$dateArray);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment