我有一系列时间表,例如:
[
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
]
我需要一个函数来查找用户是否可用的时间X和Y之间。我怎么能做到这一点?
这是我到目前为止所得到的:
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) {
//
}
}
答案 0 :(得分:1)
尝试这样的事情:
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:我会在您的位置使用http://php.net/strtotime将日期/时间字符串转换为时间戳。