我想将某个人的时间限制插入到数据库中。 例如:彼得 13:00 - 19:00
Peter 告诉我们他不能在 18:00 - 22:00 工作。 (这位于数据库中)。
因此无法将时间 13:00 - 19:00 插入数据库,因为结束时间 (19:00) 在时间范围 18:00 - 22:00 内。
如何检查?
我已经尝试了好几件事,但我不知道如何做对。
现在知道了
$van_besch = '18:00';
$tot_besch = '22:00';
$van = '13:00';
$tot = '19:00';
if (($van_besch >= $van) && ($tot_besch <= $tot)) {
$error ='<font color="red">Time '.$van_besch.' - '.$tot_besch.' Not available</font>';
}
这行不通。
感谢阅读!
最佳答案
实际上有两种情况需要检查:
$van
等于或介于$van_besch
和$tot_besch
$tot
等于或介于$van_besch
和$tot_besch
如果 $van
和 $tot
都在 $van_besch
和 $tot_besch
之间,则两种情况都为真。
此外,您还需要处理跨越一天休息时间的轮类,例如17:00 - 02:00。另一个问题是你需要处理的是 20:00 - 0:00,因为 0:00 小于 20:00。
因此我们将实时时间投影到我们自己的时间格式。
这意味着 17:00 - 02:00 将变为 17:00 - 26:00。请注意,我们需要为 $van_besch
- $tot_besch
和 $van
- $tot$
执行此操作。
在看起来像这样的代码中:
if ($tot < $van){
$tot = 24:00 - $van + $tot;
}
if ($tot_besch < $van_besch){
$tot = 24:00 - $van + $tot;
}
if (($van >= $van_besch) && ($van <= $tot_besch) ) || (($tot >= $tot_besch) && ($tot <= $tot_besch)){
... Peter is not available ...
}
关于PHP检查时间范围是否在一个时间范围内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35560536/