gpt4 book ai didi

php - 范围时间与 php 中的间隔

转载 作者:搜寻专家 更新时间:2023-10-31 22:01:25 25 4
gpt4 key购买 nike

我正在用 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());
}

最佳答案

尝试不同的时间比较方式?

$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
}

http://php.net/datetime.diff

关于php - 范围时间与 php 中的间隔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28480995/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com