{"id":17062,"date":"2012-05-24T18:23:04","date_gmt":"2012-05-25T01:23:04","guid":{"rendered":"http:\/\/css-tricks.com\/?page_id=17062"},"modified":"2012-05-24T18:23:04","modified_gmt":"2012-05-25T01:23:04","slug":"calendar-function","status":"publish","type":"page","link":"https:\/\/css-tricks.com\/snippets\/php\/calendar-function\/","title":{"rendered":"Calendar Function"},"content":{"rendered":"
<?php\r\n\r\nfunction build_calendar($month,$year,$dateArray) {\r\n\r\n    \/\/ Create array containing abbreviations of days of week.\r\n    $daysOfWeek = array('S','M','T','W','T','F','S');\r\n\r\n    \/\/ What is the first day of the month in question?\r\n    $firstDayOfMonth = mktime(0,0,0,$month,1,$year);\r\n\r\n    \/\/ How many days does this month contain?\r\n    $numberDays = date('t',$firstDayOfMonth);\r\n\r\n    \/\/ Retrieve some information about the first day of the\r\n    \/\/ month in question.\r\n    $dateComponents = getdate($firstDayOfMonth);\r\n\r\n    \/\/ What is the name of the month in question?\r\n    $monthName = $dateComponents['month'];\r\n\r\n    \/\/ What is the index value (0-6) of the first day of the\r\n    \/\/ month in question.\r\n    $dayOfWeek = $dateComponents['wday'];\r\n\r\n    \/\/ Create the table tag opener and day headers\r\n\r\n    $calendar = \"<table class='calendar'>\";\r\n    $calendar .= \"<caption>$monthName $year<\/caption>\";\r\n    $calendar .= \"<tr>\";\r\n\r\n    \/\/ Create the calendar headers\r\n\r\n    foreach($daysOfWeek as $day) {\r\n         $calendar .= \"<th class='header'>$day<\/th>\";\r\n    }\r\n\r\n    \/\/ Create the rest of the calendar\r\n\r\n    \/\/ Initiate the day counter, starting with the 1st.\r\n\r\n    $currentDay = 1;\r\n\r\n    $calendar .= \"<\/tr><tr>\";\r\n\r\n    \/\/ The variable $dayOfWeek is used to\r\n    \/\/ ensure that the calendar\r\n    \/\/ display consists of exactly 7 columns.\r\n\r\n    if ($dayOfWeek > 0) {\r\n         $calendar .= \"<td colspan='$dayOfWeek'>&nbsp;<\/td>\";\r\n    }\r\n\r\n    $month = str_pad($month, 2, \"0\", STR_PAD_LEFT);\r\n\r\n    while ($currentDay <= $numberDays) {\r\n\r\n         \/\/ Seventh column (Saturday) reached. Start a new row.\r\n\r\n         if ($dayOfWeek == 7) {\r\n\r\n              $dayOfWeek = 0;\r\n              $calendar .= \"<\/tr><tr>\";\r\n\r\n         }\r\n\r\n         $currentDayRel = str_pad($currentDay, 2, \"0\", STR_PAD_LEFT);\r\n\r\n         $date = \"$year-$month-$currentDayRel\";\r\n\r\n         $calendar .= \"<td class='day' rel='$date'>$currentDay<\/td>\";\r\n\r\n         \/\/ Increment counters\r\n\r\n         $currentDay++;\r\n         $dayOfWeek++;\r\n\r\n    }\r\n\r\n    \/\/ Complete the row of the last week in month, if necessary\r\n\r\n    if ($dayOfWeek != 7) {\r\n\r\n         $remainingDays = 7 - $dayOfWeek;\r\n         $calendar .= \"<td colspan='$remainingDays'>&nbsp;<\/td>\";\r\n\r\n    }\r\n\r\n    $calendar .= \"<\/tr>\";\r\n\r\n    $calendar .= \"<\/table>\";\r\n\r\n    return $calendar;\r\n\r\n}\r\n\r\n?><\/code><\/pre>\n

Usage<\/h4>\n

To print a table of May 2005, just do:<\/p>\n

<?php echo build_calendar(05,2005); ?><\/code><\/pre>\n

And you’ll get a table like this:<\/p>\n\n\n\n\n\n\n\n\n
May 2005<\/caption>\n
S<\/th>\nM<\/th>\nT<\/th>\nW<\/th>\nT<\/th>\nF<\/th>\nS<\/th>\n<\/tr>\n
1<\/td>\n2<\/td>\n3<\/td>\n4<\/td>\n5<\/td>\n6<\/td>\n7<\/td>\n<\/tr>\n
8<\/td>\n9<\/td>\n10<\/td>\n11<\/td>\n12<\/td>\n13<\/td>\n14<\/td>\n<\/tr>\n
15<\/td>\n16<\/td>\n17<\/td>\n18<\/td>\n19<\/td>\n20<\/td>\n21<\/td>\n<\/tr>\n
22<\/td>\n23<\/td>\n24<\/td>\n25<\/td>\n26<\/td>\n27<\/td>\n28<\/td>\n<\/tr>\n
29<\/td>\n30<\/td>\n31<\/td>\n <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n","protected":false},"excerpt":{"rendered":"

<?php function build_calendar($month,$year,$dateArray) { \/\/ Create array containing abbreviations of days of week. $daysOfWeek = array(‘S’,’M’,’T’,’W’,’T’,’F’,’S’); \/\/ 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. […]<\/p>\n","protected":false},"author":3,"featured_media":0,"parent":3233,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"page-snippet.php","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"sig_custom_text":"","sig_image_type":"featured-image","sig_custom_image":0,"sig_is_disabled":false,"inline_featured_image":false,"c2c_always_allow_admin_comments":false,"footnotes":""},"tags":[],"acf":[],"jetpack-related-posts":[{"id":4032,"url":"https:\/\/css-tricks.com\/snippets\/php\/build-a-calendar-table\/","url_meta":{"origin":17062,"position":0},"title":"Build a Calendar Table","date":"September 11, 2009","format":false,"excerpt":"","rel":"","context":"In "Article"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":192718,"url":"https:\/\/css-tricks.com\/snippets\/sass\/functional-programming-functions\/","url_meta":{"origin":17062,"position":1},"title":"Functional Programming Functions","date":"January 9, 2015","format":false,"excerpt":"For library makers and framework builders, having a couple of extra functions to dynamically apply functions to items of a list might come in handy. Here are 4 functions inspired by JavaScript and PHP equivalents, implemented in Sass. walk() The walk() function applies a given function to every item of\u2026","rel":"","context":"Similar post","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":191222,"url":"https:\/\/css-tricks.com\/snippets\/sass\/advanced-type-checking\/","url_meta":{"origin":17062,"position":2},"title":"Advanced Type Checking","date":"December 19, 2014","format":false,"excerpt":"This collection of functions is for testing if the value of a variable is of a certain type. For instance, is 13rem a relative length? TRUE! Is \"frosty the snowman\" and integer? FALSE! This is likely most useful for advanced mixin and framework creators who aim to make their code\u2026","rel":"","context":"With 2 comments","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":192767,"url":"https:\/\/css-tricks.com\/snippets\/sass\/fix-number-n-digits\/","url_meta":{"origin":17062,"position":3},"title":"Fix a Number to N Digits","date":"January 9, 2015","format":false,"excerpt":"When dealing with numbers in JavaScript or any other programming language, there is a way to truncate a number after n digits. Unfortunately, there is no such function in Sass. Still, rounding and precision issues do happen when dealing with percentage based grid systems for instance. Here is a Sass\u2026","rel":"","context":"Similar post","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":9593,"url":"https:\/\/css-tricks.com\/snippets\/javascript\/namespaced-javascript-template\/","url_meta":{"origin":17062,"position":4},"title":"Namespaced Javascript Template","date":"June 7, 2011","format":false,"excerpt":"Self invoking anonymous function assigned to the yournamespacechoice global variable. Serves the effect of keeping all functions and variables private to this function. To expose a function or variable we must explictly return it at the bottom of the function. Remaps jQuery to $. var yournamespacechoice = (function ($) {\u2026","rel":"","context":"With 3 comments","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":191217,"url":"https:\/\/css-tricks.com\/snippets\/sass\/str-replace-function\/","url_meta":{"origin":17062,"position":5},"title":"Str-replace Function","date":"December 19, 2014","format":false,"excerpt":"Sass provides a collection of handy functions to manipulate strings, however there is no function to replace a substring with another string. Here is a quick str-replace function if you ever need one. \/\/\/ Replace `$search` with `$replace` in `$string` \/\/\/ @author Kitty Giraudel \/\/\/ @param {String} $string - Initial\u2026","rel":"","context":"With 10 comments","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/pages\/17062"}],"collection":[{"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/comments?post=17062"}],"version-history":[{"count":3,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/pages\/17062\/revisions"}],"predecessor-version":[{"id":17065,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/pages\/17062\/revisions\/17065"}],"up":[{"embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/pages\/3233"}],"wp:attachment":[{"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/media?parent=17062"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/tags?post=17062"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}