$gmonth = $_GET['month'];
$gyear = $_GET['year'];
$likeDate = $gyear.'-'.str_pad($gmonth, 2, "0", STR_PAD_LEFT);
$jSon = array();
$sql = "SELECT * FROM reservations WHERE date LIKE '$likeDate%' ";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
// $jSon[$row['date']] .= '
'.$row['time_start'].'AM-'.$row['time_end'].'PMRooms: '.$row['rooms'].'
'.$row['meeting_title'].'
';
// $jSon[$row['date']] .= '
'.$row['time_start'].'AM-'.$row['time_end'].'PMRooms: '.$row['rooms'].'
'.$row['meeting_title'].'
';
$jSon[$row['date']] .= '
'.$row['time_start'].'AM - '.$row['time_end'].'PMRooms: '.$row['rooms'].'
'.$row['meeting_title'].'
';
}
}
$currentMonth = $gmonth;
$currentYear = $gyear;
$preMonth = $currentMonth == 1 ? 12 : intval($currentMonth)-1;
$add0PreMonth = str_pad($preMonth, 2, "0", STR_PAD_LEFT);
$preYear = $currentMonth == 1 ? intval($currentYear)-1 : $currentYear;
$nextMonth = $currentMonth == 12 ? 1 : intval($currentMonth)+1;
$add0nextMonth = str_pad($nextMonth, 2, "0", STR_PAD_LEFT);
$nextYear = $currentMonth == 12 ? intval($currentYear)+1 : $currentYear;
echo '
< Pre '.$gmonth.' '.$gyear.' Next >
';
echo draw_calendar($gmonth,$gyear,$jSon);
function draw_calendar($month,$year,$jSon){
/* draw table */
$calendar = '
';
/* table headings */
$headings = array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
$calendar.= ''.implode(' | ',$headings).' |
';
/* days and weeks vars now ... */
$running_day = date('w',mktime(0,0,0,$month,1,$year));
$days_in_month = date('t',mktime(0,0,0,$month,1,$year));
$days_in_this_week = 1;
$day_counter = 0;
$dates_array = array();
$thisDay = date("Y-m");
$toDay = date("Y-m-d");
/* row for week one */
$calendar.= '';
/* print "blank" days until the first of the current week */
for($x = 0; $x < $running_day; $x++):
$calendar.= ' | ';
$days_in_this_week++;
endfor;
/* keep going with days.... */
for($list_day = 1; $list_day <= $days_in_month; $list_day++):
if($toDay == ($year.'-'.str_pad($month, 2, "0", STR_PAD_LEFT).'-'.str_pad($list_day, 2, "0", STR_PAD_LEFT))){
$calendar.= '';
}else{
$calendar.= ' | ';
}
/** QUERY THE DATABASE FOR AN ENTRY FOR THIS DAY !! IF MATCHES FOUND, PRINT THEM !! **/
foreach($jSon as $x=>$x_value)
{
$newEventDate = $year.'-'.str_pad($month, 2, "0", STR_PAD_LEFT).'-'.str_pad($list_day, 2, "0", STR_PAD_LEFT);
if($newEventDate == $x){
$calendar.= $x_value;
// $calendar.= ' '.$newEventDate.' '.$x.' --- '.$x_value.' ';
}
}
/* add in the day number */
$calendar.= ''. $list_day. ' ';
$calendar.= ' | ';
if($running_day == 6):
$calendar.= '
';
if(($day_counter+1) != $days_in_month):
$calendar.= '';
endif;
$running_day = -1;
$days_in_this_week = 0;
endif;
$days_in_this_week++; $running_day++; $day_counter++;
endfor;
/* finish the rest of the days in the week */
if($days_in_this_week < 8):
for($x = 1; $x <= (8 - $days_in_this_week); $x++):
$calendar.= ' | ';
endfor;
endif;
/* final row */
$calendar.= '
';
/* end the table */
$calendar.= '
';
/* all done, return result */
return $calendar;
}
?>