Reputation: 1172
I have an array of timeframes, for example these:
[
start_time => 09:00,
end_time => 11:00
available => 1
],
[
start_time => 11:00,
end_time => 12:00
available => 0
],
[
start_time => 12:00,
end_time => 18:00,
available => 1
]
I need a function that finds if between times X and Y the user is available or not. How could I achieve that?
This is what I got so far:
private function findTimeframe($timeframes, $startTime, $endTime)
{
if (count($timeframes) == 0) {
return false;
}
if ($timeframes[0]['start_time'] < $startTime && $timeframes[0]['end_time'] < $startTime) {
array_shift($timeframes);
return $this->findTimeframe($timeframes, $startTime, $endTime);
}
if ($timeframes[0]['end_time'] < $endTime) {
//
}
}
Upvotes: 0
Views: 24
Reputation: 56
Try something like this:
private function findTimeframe($timeframes, $startTime, $endTime)
{
foreach ($timeframes as $timeframe) {
if ($timeframe['available']) {
continue; // if timeframe is avaiable, skip checking times
}
if (// $startTime is in the timeframe
($timeframe['start_time'] < $startTime && $startTime < $timeframe['end_time']) //
// or $endTime is in the timeframe
|| ($timeframe['start_time'] < $endTime && $endTime < $timeframe['end_time'])
) {
return false;
}
}
return true; // not found any timeframe with available=0 and within $startTime / $endTime range
}
BTW: I would use http://php.net/strtotime in your place to convert date/time strings into timestamps.
Upvotes: 1