Okay, so I have a start datetime and an end datetime for multiple items. I need to find out if there are more than 3 items that are within the same datetime as each other. I just can’t wrap my head around this one, I can get the days of each individual item into an array using this function
function getDatesBetween2Dates($startTime, $endTime) {
$day = 86400;
$format = 'm/d/y g:i A';
$startTime = strtotime($startTime);
$endTime = strtotime($endTime);
$numDays = round(($endTime - $startTime) / $day) + 1;
$days = array();
for ($i = 0; $i < $numDays; $i++) {
$days[] = date($format, ($startTime + ($i * $day)));
}
return $days;
}
The problem is, it only get the first time and just adds a full day to every day afterward. it doesn’t take into account for the last days time. I just can’t think of an efficient way to do this.
Just to clarify, let me give you an example. I have 5 items in my table, they each have a datetime range.. start and end. I need to check if 4 or more of those items have any days in their date range that are the same. Can anyone point me in the right direction? Thanks.
Change
for ($i = 0; $i < $numDays; $i++) {tofor ($i = 0; $i <= $numDays; $i++) {to get all dates in the range, including the last date.Unfortunately you’re going about this the wrong way. Here’s some pseudocode of how I would do it:
I don’t believe this is the most efficient way, but unless you’re dealing with huge date ranges or quite a large number of dates, it will be relatively fast.