Jump to content

fileparts

New Members
  • Posts

    4
  • Joined

  • Last visited

fileparts's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. Okay thanks @barand for the answer, I've tweaked and added bits to get it to display as a calendar. Here's what I've got: $dayLabels = array("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"); $dayMiniLabels = array("Mon","Tue","Wed","Thu","Fri","Sat","Sun"); $monthLables = array("January","February","March","April","May","June","July","August","September","October","November","December"); $forceMonth = $_GET['m']; $forceYear = $_GET['y']; if(isset($forceMonth)) { if(strlen($forceMonth) == 1) { $forceMonth = sprintf("%02d", $forceMonth); }; $currentMonth = $forceMonth; } else { $currentMonth = date("m"); }; if(isset($forceYear)) { if(strlen($forceYear) == 2) { $dt = DateTime::createFromFormat('y', $forceYear); $forceYear = $dt->format('Y'); }; $currentYear = $forceYear; } else { $currentYear = date("Y"); }; $monthStart = date($currentYear. '-' .$currentMonth. '-01'); $monthEnd = date($currentYear. '-' .$currentMonth. '-t'); $prevMonth = sprintf("%02d", $currentMonth - 1); $nextMonth = sprintf("%02d", $currentMonth + 1); $prevYear = sprintf("%02d", $currentYear - 1); $nextYear = sprintf("%02d", $currentYear + 1); $firstDayofMonth = date("D", $currentDate); $firstDayofMonth = array_search($firstDayofMonth, $dayMiniLabels); $firstDayofMonth = $firstDayofMonth; $bookings = array(); $s = new DateTime($monthStart); $e = new DateTime("$monthEnd + 1 days"); $oneday = new DateInterval('P1D'); $dp = new DatePeriod($s, $oneday, $e); foreach ($dp as $d) { $bookings[$d->format('Y-m-d')] = ''; }; $sql = "SELECT userID , bookingStart , bookingEnd FROM bookings WHERE machineID = ? AND bookingStart < ? AND bookingEnd > ?"; $getBookings = $con->prepare($sql); $getBookings->bind_param('iss', $_GET['id'], $monthEnd, $monthStart); $getBookings->execute(); $getBookings->bind_result($uid, $bstart, $bend); while ($getBookings->fetch()) { $s = new DateTime(max($bstart, $monthStart)); $e = new DateTime(min($bend, $monthEnd)); $e->modify('+1 day'); $dp = new DatePeriod($s, $oneday, $e); foreach ($dp as $d) { $bookings[$d->format('Y-m-d')] = $uid; }; }; $dayCount = 0; $startMonth = 0; $calDate = 0; echo '<table>'; foreach($bookings as $date) { $dayCount++; $calDate++; $calDate = sprintf("%02d", $calDate); if($dayCount == 1) { echo '<tr>'; }; if($firstDayofMonth != 7) { while($startMonth < $firstDayofMonth) { echo '<td class="padding"></td>'; $startMonth++; $dayCount++; $temp_dayCount = sprintf("%02d", $dayCount); $dayCount = $temp_dayCount; }; }; echo '<td>' .$calDate. '</td>'; if($dayCount == 7) { echo '</tr>'; $dayCount = 0; }; }; echo '</table>'; Any changes that I should make?
  2. Thanks for the great response. I still would need to put this in a tabular format, 7 columns etc, to make a calendar which I would be able to read from. How would you go about outputting the data so that it can be read within a html table?
  3. Thanks for the response, certainly an interesting idea. How about a way to check to see if a bookedStart date and bookedEnd date have the same position in their respective arrays and then checking if bookedEnd date's month is greater than bookedStart date's month, then - somehow, the dates between these two dates become highlighted? Possibly simplar than your answer, although I wouldn't know where to start with coding it.
  4. I am currently working on a Calendar where it will be populated from Arrays that will be populated from a database. I currently have the Calendar look for a start date that is within the bookedStart Array and then set the variable booked as true until it finds an end date that is within the bookedEnd Array and then set the variable to false. The issue with this method is that it will only show what dates are booked, between a start and end date, if the start and end date are within the same month. How would I adapt my Calendar to allow it to work across months, for example: Here is the code I am working with: My Main Variables: //Labels $dayLabels = array("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"); $dayMiniLabels = array("Mon","Tue","Wed","Thu","Fri","Sat","Sun"); $monthLables = array("January","February","March","April","May","June","July","August","September","October","November","December"); //max values $maxDays = 7; $maxMonths = 12; //stats $forceMonth = $_GET['m']; $forceYear = $_GET['y']; $todayDate = date("d-m-Y"); $todayDate = date("d-m-Y", strtotime($todayDate)); $explodeToday = explode("-", $todayDate); $currentDay = $explodeToday[0]; if(isset($forceMonth)) { $currentMonth = $forceMonth; } else { $currentMonth = $explodeToday[1]; }; if(isset($forceYear)) { $currentYear = $forceYear; } else { $currentYear = $explodeToday[2]; }; $currentDate = strtotime("01-$currentMonth-$currentYear"); $prevMonth = sprintf("%02d", $currentMonth - 1); $nextMonth = sprintf("%02d", $currentMonth + 1); $prevYear = sprintf("%02d", $currentYear - 1); $nextYear = sprintf("%02d", $currentYear + 1); $daysInMonth = cal_days_in_month(CAL_GREGORIAN, $currentMonth, $currentYear); $firstDayofMonth = date("D", $currentDate); $firstDayofMonth = array_search($firstDayofMonth, $dayMiniLabels); $firstDayofMonth = $firstDayofMonth; //database values $bookedStart = array(); $bookedEnd = array(); $bookedUser = array(); if($getBookings = $con->prepare("SELECT userID,bookingStart,bookingEnd FROM bookings WHERE machineID=?")) { $getBookings->bind_param("i", $_GET['id']); if($getBookings->execute()) { $getBookings->bind_result($bookingUserID,$bookingStart,$bookingEnd); while($getBookings->fetch()) { array_push($bookedStart, $bookingStart); array_push($bookedEnd, $bookingEnd); array_push($bookedUser, $bookingUserID); }; }; }; $getBookings->close(); //counters $daysIntoMonth = 0; $dayCounter = 0; $startMonth = 0; Here is the display code, "where the issue is": <table class="full grid dayLabels"> <tr> <?php foreach($dayLabels as $day) { echo '<td class="day"><p>' .$day. '</p></td>'; }; ?> </tr> </table> <table id="calendar" class="full grid calendar"> <?php while($daysIntoMonth < $daysInMonth) { //days into month $daysIntoMonth++; $temp_intoMonth = sprintf("%02d", $daysIntoMonth); $daysIntoMonth = $temp_intoMonth; //days into week $dayCounter++; $temp_dayCounter = sprintf("%02d", $dayCounter); $dayCounter = $temp_dayCounter; //current calendar date $calDate = date('d-m-Y', strtotime($daysIntoMonth. '-' .$currentMonth. '-' .$currentYear)); $calTime = strtotime($calDate); $todaysNumber = date('w', $timeCal); if($dayCounter == 1) { echo '<tr>'; }; if($firstDayofMonth != 7) { while($startMonth < $firstDayofMonth) { echo '<td class="padding"></td>'; $startMonth++; $dayCounter++; $temp_dayCounter = sprintf("%02d", $dayCounter); $dayCounter = $temp_dayCounter; }; }; if($startKey = in_array($calDate, $bookedStart, true)) { $booked = true; echo ' <td class="booked"> <p class="date">' .$daysIntoMonth. '</p> </td> '; } else if(in_array($calDate, $bookedEnd, true)) { $booked = false; echo ' <td class="booked"> <p class="date">' .$daysIntoMonth. '</p> </td> '; } else if($booked == true) { echo ' <td class="booked"> <p class="date">' .$daysIntoMonth. '</p> </td> '; } else { echo ' <td> <p class="date">' .$daysIntoMonth. '</p> </td> '; }; if($dayCounter == $maxDays) { echo '</tr>'; $dayCounter = 0; }; }; ?> </table> tl;dr - if a booking runs over a month, the start date isn't shown on the ending month, but the end date is, because of how I have it check between dates, it won't highlight the days before the end date. Note: This is on an internal server, so it doesn't have to worry about SQL Injection. Thanks in advance, fp
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.