PHP检查一个日期范围是否发生在另一个日期范围内
我需要做的是检查“任务”是否在午餐时间发生。我有4个datetime对象。我找到的最接近的代码如下,但没有涵盖所有可能的场景,例如,任务的开始时间可能是午餐的一半PHP检查一个日期范围是否发生在另一个日期范围内,php,datetime,time,comparison,Php,Datetime,Time,Comparison,我需要做的是检查“任务”是否在午餐时间发生。我有4个datetime对象。我找到的最接近的代码如下,但没有涵盖所有可能的场景,例如,任务的开始时间可能是午餐的一半 if (($thisStart <= $lunchStart) && ($thisEnd >= $lunchStart)) { //happen at same time } if($thisStart=$午餐开始)){ //同时发生 } 谢谢 Alex这里您需要这个逻辑 将时间改为秒,这
if (($thisStart <= $lunchStart) && ($thisEnd >= $lunchStart)) {
//happen at same time
}
if($thisStart=$午餐开始)){
//同时发生
}
谢谢
Alex这里您需要这个逻辑
将时间改为秒,这样就容易了 但是你的日期应该是“y/m/d”格式
$thisStart_1 = strtotime($thisStart);
$lunchStart_1 = strtotime($lunchStart);
$thisEnd_1 = strtotime($thisEnd);
您的时间将如下所示,例如:-2014-04-30------->1398808800(秒) 因此,您可以在这里以秒为单位计算时间,以便检查时间管理。
if (($thisStart_1 <= $lunchStart_1) && ($thisEnd_1 >= $lunchStart_1)) {
//happen at same time
}
更改的日期格式,使其可由strotime函数使用
$date = date_create_from_format('d/m/Y', $gDate);
回音孔检查真正的格式
$askfrmt = date_format($date, 'Y-m-d');
指定日期的strotime函数
$askfrmta = strtotime($askfrmt);
如果($thisStart>=$午餐开始时间和$thisStart=$午餐开始时间和$thisEnd我不确定是否理解了您的问题,但是如果您想知道thisStart
和thisEnd
是否都在午餐时间,请尝试以下方法:
if (!($thisStart > $lunchEnd || $thisEnd < $lunchStart) ){
// Occurs during lunch
}
如果(!($thisStart>$午餐结束| |$thisEnd<$午餐开始)){
//发生在午餐时间
}
My code还将检查任务是否在午餐前开始,但是否在午餐期间结束!
if($thisStart >= $lunchStart && $thisStart <= $lunchEnd)
{
//occurs during lunch
} else if ($thisEnd >= $lunchStart && $thisEnd <= $lunchEnd)
{
//occurs during lunch
}
if (!($thisStart > $lunchEnd || $thisEnd < $lunchStart) ){
// Occurs during lunch
}