FullCalendar: Changing event colors with comparing dates

Viewed 44

Recently, ive started tyring out arshaw's fullcalendar. Along the developing journey, i was searching through websites on solution to get the event color to change based on the dates of the event and the current date. This code is written with HTML and PHP with Javascript.

This is an answer to my own questions posts based on what i achieve as there is no similar questions with solutions to this.

So below is my answer and how the FullCalendar looks like.

Example of working FullCalendar

1 Answers

To clarify, i am using odbc with microsoft Access as database.

My way of doing this is as below:

Events at fullcalendar script

  events: [
        <?php 
            include 'connect.php'; //connect to database

      function getColor($date) {
        $currentDate = date('Y-m-d');
        $oneweekDate = date('Y-m-d',strtotime('-1 week')); // this part is to compare with the date 1 week ago
        $eventColor = '';
        if ($date == $currentDate) {
            $eventColor = '#fb8c00';
        } else if($date > $oneweekDate && $date < $currentDate){
            $eventColor = '#ff0000';
        } else if($date < $oneweekDate){
            $eventColor = '#696969';
        } else {
            $eventColor = '#008000';
        }
        return $eventColor;
    }

    $sql="SELECT * FROM masterlist1";
    $result=odbc_exec($conn,$sql);
        while($row=odbc_fetch_array($result)){
            $newEnd = date("Y-m-d", strtotime($row['Calibration_Due_Date']));
            $color = getColor($newEnd); //store the date from database into a PHP variable and then call the PHP function getColor to get return result
        ?>
       {  
        title: '--title--',  <!--u may get info from fullcalendar.io on the documentations for these parts-->
        start: '--start date--', 
        end: '--end date--', 
        description : '--description--',
        color : '<?php echo $color?>' <!-- this part is where we get the return result from the getColor function and store it into $color variable and then echo it out here for the event color.-->
      }, 
      <?php } ?>
      ],
Related