Sunday, July 16, 2017

Calendar made by PHP

Usage:

To display the calendar:
$c = new CalendarView();
$c->makeCalendar("12", "2017"); //Display the calendar
To display clickable events in the calendar:
$events['15'][] = ['event' => 'Meet my family', 'eventClassName'=>'ColorGreen', 'id'=>'1'];
// this will generete:
//<a id="1" class="ColorGreen" href="">Meet my family</a> in 15th of the month on the calendar.
$c = new CalendarView();
$c->makeCalendar("12", "2017", $events); //Display the calendar

Code: 


<?php
$c = new CalendarView();
//$events[Which day of the month here][] = [event title here, the classs name attribute of the event element here, id attribute here]
$events['15'][] = ['event' => 'Meet my family', 'eventClassName'=>'ColorGreen', 'id'=>'1'];
// this will generete:
//<a id="1" class="ColorGreen" href="">Meet my family</a> in 15th of the month.

$c->makeCalendar("12", "2017", $events); //makeCalendar(Month, Year, event array)

class CalendarView {
    public function initialize()
    {
        date_default_timezone_set('America/Los_Angeles');
    }
 
    public function makeCalendar($month, $year, $eventArray=[]){
        settype($days, "integer" );
        $days = cal_days_in_month(CAL_GREGORIAN, $month, $year);
        $weekDayArray = ["0"=>"Sun", "1"=>"Mon", "2"=>"Tue", "3"=>"Wed", "4"=>"Thu", "5"=>"Fri", "6"=>"Sat"];
     
        echo "<table class='calendar'><tr>";
        settype($i, "integer" );
        for($i=0;$i<7;$i++){
             echo "<td class='weekdays'>".$weekDayArray[$i]."</td>";
        }
        echo "</tr><tr>";
     
        $firstdate = $year.'-'.$this->startWithZero($month).'-01';
        $firstDaysWeekDay = $this->getWeekday($firstdate);
       
        settype($i, "integer" );
        settype($j, "integer" );
        $weekdaynum = 0;
        for($i=1,$j=1;$j<=$days;$i++){
            if($i<8){
                if($firstDaysWeekDay <= $i-1){
                    $event = $this->setEvent($eventArray, $j);
                    echo "<td>".$j.$event."</td>";
                    if($i-1 === 6){
                        echo "</tr><tr>";
                    }
                    $j++;
                    continue;
                }else{
                    echo "<td></td>";
                    continue;
                }
            }else{
                $event = $this->setEvent($eventArray, $j);
                echo "<td>".$j.$event."</td>";
                $weekdaynum++;
                $j++;
            }
            if($weekdaynum%7===0) echo "</tr><tr>";
        }
        echo "</tr></table>";
    }

    private function getWeekday($date) {
        return date('w', strtotime($date));
    }

    private function startWithZero($num){
        if(is_numeric($num) && strlen($num) < 2){
            return "0".$num;                
        }else{
            return $num;        
        }
    }
 
    private function setEvent($eventArray, $j){
        $event = "";
        if(isset($eventArray[$this->startWithZero($j)])){
            if(is_array($eventArray[$this->startWithZero($j)])){
                foreach($eventArray[$this->startWithZero($j)] as $value){
                    $link = ''; //Link for each events here.
                    $event .= "<div class='event-wrapper'><a id='".$value['id']."' class='".$value['eventClassName']."' href='".$link."'>".$value['event']."</a></div>";
                }
            }
        }
        return $event;
    }
 
}



Result:


Calendar table is generated:
<table class="calendar">
    <tbody>
        <tr>
            <td class="weekdays">Sun</td>
            <td class="weekdays">Mon</td>
            <td class="weekdays">Tue</td>
            <td class="weekdays">Wed</td>
            <td class="weekdays">Thu</td>
            <td class="weekdays">Fri</td>
            <td class="weekdays">Sat</td>
        </tr>
        <tr>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td>1</td>
            <td>2</td>
        </tr>
        <tr>
            <td>3</td>
            <td>4</td>
            <td>5</td>
            <td>6</td>
            <td>7</td>
            <td>8</td>
            <td>9</td>
        </tr>
        <tr>
            <td>10</td>
            <td>11</td>
            <td>12</td>
            <td>13</td>
            <td>14</td>
            <td>15
                <br />
                <div class="event-wrapper">
                    <a class="ColorGreen" href="" id="1">Meet my family</a>
                </div>
            </td>
            <td>16</td>
        </tr>
        <tr>
            <td>17</td>
            <td>18</td>
            <td>19</td>
            <td>20</td>
            <td>21</td>
            <td>22</td>
            <td>23</td>
        </tr>
        <tr>
            <td>24</td>
            <td>25</td>
            <td>26</td>
            <td>27</td>
            <td>28</td>
            <td>29</td>
            <td>30</td>
        </tr>
        <tr>
            <td>31</td>
        </tr>
    </tbody>
</table>

capture of the calendar

Licence:

CC0 Public Domain
Free for commercial use