php中带间隔的范围时间

php中带间隔的范围时间,php,datetime,Php,Datetime,我正在用PHP创建一个每日计划表,每30分钟写一行表格,检查约会是否匹配。然而,我在预约时间部分遇到了问题,例如,我必须检查时间:15:34:58-16:34:58,我无法在15:30和16:00之间找到匹配项。这是我的密码: try { $inizio=new DateTime($r["data"]." ".$r["ora"]); // for me is 15:34:58 $fine=new DateTime($r["data_fin"]." ".$r["ora_fin"])

我正在用PHP创建一个每日计划表,每30分钟写一行表格,检查约会是否匹配。然而,我在预约时间部分遇到了问题,例如,我必须检查时间:
15:34:58-16:34:58
,我无法在
15:30
16:00
之间找到匹配项。这是我的密码:

try {
    $inizio=new DateTime($r["data"]." ".$r["ora"]); // for me is 15:34:58
    $fine=new DateTime($r["data_fin"]." ".$r["ora_fin"]); // for me is 16:34:58
    $timetoCheck=new DateTime(date("Y-m-d")." ".$h.":".$m.":00"); //dinamically change in 15:30 16:00 and so on..
    $intervallo=new DateInterval("PT30M");
    $range=new DatePeriod($inizio, $intervallo, $fine);
    foreach ($range as $fase){
        if (($timetoCheck>=$inizio) and ($timetoCheck<=$fine)) {
            echo $fase->format("H:i:s")." ". $inizio->format("H:i:s")." - ".$fine->format("H:i:s")."<br/><a href='#' id='evento_".$r["key_id"]."'>".$r["titolo"]."</a><br/>";
        }
    }
} catch (Exception $ecc){
    $logger->error($ecc->getMessage());
}
试试看{
$r[“data”]。$r[“ora”];//对我来说是15:34:58
$fine=newdatetime($r[“data\u fin”]。$r[“ora\u fin”]);//对我来说是16:34:58
$timetoCheck=新日期时间(日期(“Y-m-d”)。“$h:”$m:“:00”);//在15:30到16:00之间进行友好更改,依此类推。。
$intervallo=新日期间隔(“PT30M”);
$range=新的日期周期($INGISIO、$intervallo、$fine);
foreach(范围为$fase){
如果($timetoCheck>=$INGISIO)和($timetoCheckformat(“H:i:s”)。$INGISIO->格式(“H:i:s”)。“-”$fine->格式(“H:i:s”)。“

”; } } }捕获(例外$ecc){ $logger->error($ecc->getMessage()); }
尝试另一种比较时间的方法

$dateTime = new DateTime("15:40:10");
$startTime = new DateTime("15:30:00");
$endTime = new DateTime("16:00:00");
if ($dateTime->diff($startTime)->format('%R') == '-' && $dateTime->diff($endTime)->format('%R') == '+') {
    // current time is within the appointment period
} else {
    // current time is either before or after the appointment period
}

如果你想让任何人帮忙,请从这张纸上剪下相关代码。如果没有任何内容,你可能不需要每30分钟写一行。@James我友好地检查了一下,我无法提前知道我是否能找到什么,但如果从15:34开始,为什么我的if与15:30-16:00之间的约会不匹配?你可以n通过条件向其添加处理。如果它是
null
,则它不返回任何事件。您会发现,如果您在一年内每隔30分钟添加一行,那么在不需要时,您将得到17531行。