{"id":1208,"date":"2012-06-08T07:27:42","date_gmt":"2012-06-08T07:27:42","guid":{"rendered":"http:\/\/truelogic.org\/wordpress\/?p=1208"},"modified":"2012-06-08T07:29:49","modified_gmt":"2012-06-08T07:29:49","slug":"sample-calendar-script-in-php","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2012\/06\/08\/sample-calendar-script-in-php\/","title":{"rendered":"Sample Calendar Script in PHP"},"content":{"rendered":"            <script type=\"text\/javascript\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/plugins\/wordpress-code-snippet\/scripts\/shBrushPhp.js\"><\/script>\n<div>\n<figure id=\"attachment_1218\" aria-describedby=\"caption-attachment-1218\" style=\"width: 1024px\" class=\"wp-caption alignleft\"><a href=\"https:\/\/truelogic.org\/wordpress\/2012\/06\/08\/sample-calendar-script-in-php\/calendar\/\" rel=\"attachment wp-att-1218\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1218\" title=\"calendar\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2012\/06\/calendar.png\" alt=\"\" width=\"1024\" height=\"738\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2012\/06\/calendar.png 1024w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2012\/06\/calendar-300x216.png 300w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption id=\"caption-attachment-1218\" class=\"wp-caption-text\">Calendar Script<\/figcaption><\/figure>\n<p>There are lots of php based calendar scripts out there, so this one is nothing new. \u00a0This 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\u00a0assumes that the year is always the current year. This can be easily changed by passing it as an argument.<\/p>\n<p>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:<\/p>\n<ul>\n<li>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<\/li>\n<li>Calculate starting weekday, ending weekday, no.of days in the month<\/li>\n<li>Create a double dim array corresponding to the UI grid of the calendar table. 6 rows of 7 columns each.<\/li>\n<li>As per the current month specs, fill in the required cells of the array and leave the rest blank<\/li>\n<li>Once the array is initialized, run a loop through it to show the output in a grid.<\/li>\n<li style=\"text-align: left;\">Each date cell has been checked for a click event in javascript , using simple jQuery.<\/li>\n<\/ul>\n<\/div>\n<div style=\"text-align: center;\"><a title=\"Demo page\" href=\"http:\/\/truelogic.org\/calendar.php\" target=\"_blank\">The demo page of this code is here<\/a><\/div>\n<div style=\"text-align: center;\"><a title=\"Download calendar file\" href=\"http:\/\/www.truelogic.org\/calendar.zip\" target=\"_blank\">The php script can be downloaded here<\/a><\/div>\n<div style=\"text-align: left;\">The php initialization code snippet is given below:<\/div>\n<p><pre class=\"brush: php\">\t\t\/\/ get params\r\n\t$monthArg = $_GET[&#039;m&#039;];\r\n\r\n\t\r\n\t\t\t\t\t\t\/\/ calc dates and weekdays\r\n\t\t\t\tif ($monthArg == null || $monthArg == &#039;&#039;)\t\t\r\n\t\t\t\t\t$currMonth= date(&quot;m&quot;);\t\t\t\t\r\n\t\t\t\telse\r\n\t\t\t\t\t$currMonth = intval($monthArg);\t\r\n\t\t\t\t\t\t\t\t\r\n\t\t\t\t$currYear = date(&quot;Y&quot;);\r\n\t\t\t\t$startDate = strtotime($currYear . &quot;-&quot; . $currMonth . &quot;-01 00:00:01&quot;);\r\n\t\t\t\t$startDay= date(&quot;N&quot;, $startDate);\r\n\t\t\t\t$monthName = date(&quot;M&quot;,$startDate );\r\n\t\t\t\t\r\n\t\t\t\t\/\/echo(&quot;start day=&quot;. $startDay . &quot;&lt;br&gt;&quot;);\r\n\r\n\t\t\t\t$daysInMonth = cal_days_in_month(CAL_GREGORIAN, date(&quot;m&quot;, $startDate), date( &quot;Y&quot;, $startDate));\r\n\t\t\t\t$endDate = strtotime($currYear . &quot;-&quot; . $currMonth . &quot;-&quot; .  $daysInMonth .&quot; 00:00:01&quot;);\r\n\r\n\t\t\t\t\/\/echo(date(&quot;Y&quot;, $endDate) . &quot;-&quot; . date(&quot;m&quot;, $endDate) . &quot;-&quot;. date(&quot;d&quot;, $endDate));\r\n\t\t\t\t$endDay = date(&quot;N&quot;, $endDate);\r\n\t\t\t\t\/\/echo(&quot;end day=&quot; . $endDay . &quot;&lt;br&gt;&quot;);\t\t\r\n\r\n\t\t\t\t\/\/ initialize structure array matching the calendar grid\r\n\t\t\t\t\/\/ 6 rows and 7 columns\r\n\r\n\t\t\t\t\t\/\/ php date sunday is zero\r\n\t\t\t\tif ($startDay&gt; 6)\r\n\t\t\t\t\t$startDay = 7 -$startDay;\r\n\r\n\t\t\t\t$currElem = 0;\r\n\t\t\t\t$dayCounter = 0;\r\n\t\t\t\t$firstDayHasCome = false;\r\n\t\t\t\t$arrCal = array();\r\n\t\t\t\tfor($i = 0; $i &lt;= 5; $i ++) {\r\n\t\t\t\t\tfor($j= 0; $j &lt;= 6; $j++) {\r\n\t\t\t\t\t\t\t\t\/\/ decide what to show in the cell\r\n\t\t\t\t\t    if($currElem &lt; $startDay &amp;&amp; !$firstDayHasCome)\t\t\t\r\n\t\t\t\t\t\t\t$arrCal[$i][$j] = &quot;&quot;;\r\n\t\t\t\t\t\telse if ($currElem == $startDay &amp;&amp; !$firstDayHasCome) {\r\n\t\t\t\t\t\t\t$firstDayHasCome= true;\r\n\t\t\t\t\t\t\t$arrCal[$i][$j] = ++$dayCounter;\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t\telse if ($firstDayHasCome) {\r\n\t\t\t\t\t\t\tif ($dayCounter &lt; $daysInMonth)\r\n\t\t\t\t\t\t\t\t$arrCal[$i][$j] = ++ $dayCounter; \r\n\t\t\t\t\t\t\telse\r\n\t\t\t\t\t\t\t\t$arrCal[$i][$j] = &quot;&quot;;\t\r\n\t\t\t\t\t\t}\t\t\t\t\t\t\t\r\n\t\t\t\t\r\n\t\t\t\t\t\t$currElem ++;\t\t\t\t\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\r\n\t\t?&gt;\r\n<\/pre><\/p>\n<div style=\"text-align: left;\">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.<\/div>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>There are lots of php based calendar scripts out there, so this one is nothing new. \u00a0This is just my contribution to the list out <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2012\/06\/08\/sample-calendar-script-in-php\/\" title=\"Sample Calendar Script in PHP\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":1218,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,10,11],"tags":[],"class_list":["post-1208","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-apachephp","category-programming","category-tutorials"],"_links":{"self":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/1208","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/comments?post=1208"}],"version-history":[{"count":18,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/1208\/revisions"}],"predecessor-version":[{"id":1225,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/1208\/revisions\/1225"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media\/1218"}],"wp:attachment":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media?parent=1208"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=1208"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=1208"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}