Last active
September 4, 2017 20:39
-
-
Save agarzon/7089504 to your computer and use it in GitHub Desktop.
Calendar generator function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'> </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'> </td>"; | |
} | |
$calendar .= "</tr>"; | |
$calendar .= "</table>"; | |
return $calendar; | |
} | |
echo build_calendar(11, 2013); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Replace line 28 with this to highlight the current date