Skip to content

Instantly share code, notes, and snippets.

@agarzon
Last active September 4, 2017 20:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save agarzon/7089504 to your computer and use it in GitHub Desktop.
Save agarzon/7089504 to your computer and use it in GitHub Desktop.
Calendar generator function
<?php
function build_calendar($month, $year) {
$daysOfWeek = array('S','M','T','W','T','F','S');
$firstDayOfMonth = mktime(0,0,0,$month,1,$year);
$numberDays = date('t',$firstDayOfMonth);
$dateComponents = getdate($firstDayOfMonth);
$monthName = $dateComponents['month'];
$dayOfWeek = $dateComponents['wday'];
$calendar = "<table class='calendar'>";
$calendar .= "<caption>$monthName $year</caption>";
$calendar .= "<tr>";
foreach($daysOfWeek as $day) {
$calendar .= "<th class='header'>$day</th>";
}
$currentDay = 1;
$calendar .= "</tr><tr>";
if ($dayOfWeek > 0) {
$calendar .= "<td colspan='$dayOfWeek'>&nbsp;</td>";
}
$month = str_pad($month, 2, "0", STR_PAD_LEFT);
while($currentDay <= $numberDays){
if($dayOfWeek == 7){
$dayOfWeek = 0;
$calendar .= "</tr><tr>";
}
$currentDayRel = str_pad($currentDay, 2, "0", STR_PAD_LEFT);
$date = "$year-$month-$currentDayRel";
$calendar .= "<td class='day' rel='$date'>$currentDay</td>";
$currentDay++;
$dayOfWeek++;
}
if($dayOfWeek != 7){
$remainingDays = 7 - $dayOfWeek;
$calendar .= "<td colspan='$remainingDays'>&nbsp;</td>";
}
$calendar .= "</tr>";
$calendar .= "</table>";
return $calendar;
}
echo build_calendar(11, 2013);
@xeoncross
Copy link

Replace line 28 with this to highlight the current date

    // Is this today?
    if(date('Y-m-d') == $date) {
        $calendar .= "<td class='day success' rel='$date'><b>$currentDay</b></td>";
    } else {
        $calendar .= "<td class='day' rel='$date'>$currentDay</td>";
    }

@YukiSuHninWai
Copy link

Thank u for ur code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment