简体   繁体   English

PHP检查日期范围是否发生在另一个日期范围内

[英]PHP Check if a date range happens during another date range

What I need to do is check a "task" does not occur during a lunch hour. 我需要做的是检查午餐时间内不会发生“任务”。 I have 4 datetime objects. 我有4个日期时间对象。 The closest code I have found is below but does not cover all possible scenarios, for example the start of the task could be halfway through lunch. 我找到的最接近的代码在下面,但没有涵盖所有可能的情况,例如任务的开始可能是午餐的中途。

 if (($thisStart <= $lunchStart) && ($thisEnd >= $lunchStart)) {
     //happen at same time
 }

Thanks 谢谢

Alex 亚历克斯

Here you need this logic 在这里你需要这个逻辑
Change time to second so it will be easy 将时间改为秒,这样很容易

but your date should be in ("y/m/d") format 但是你的日期应该是(“y / m / d”)格式

$thisStart_1 = strtotime($thisStart);


$lunchStart_1 = strtotime($lunchStart);  


$thisEnd_1 = strtotime($thisEnd);    

your Time will look like that for example:- 你的时间将如下所示: -
2014-04-30------->1398808800 (in second) 2014-04-30 -------> 1398808800(第二名)

So here you you get your time on second so it will be easy to check the time managment.. 所以,在这里,你得到你的时间,所以很容易检查时间管理..

if (($thisStart_1 <= $lunchStart_1) && ($thisEnd_1 >= $lunchStart_1)) {
     //happen at same time
 }

IF YOUR DATE IN DIFFERENT FORMAT THEN USE THIS:- 如果您的日期格式不同,请使用此方式: -

date in your format-*/ 格式的日期 - * /

$gDate='30/04/2014';//whatever format here put same in this "date_create_from_format" function

change date format for make it usable by strtotime function 更改日期格式以使其可通过strtotime函数使用

 $date = date_create_from_format('d/m/Y', $gDate);

echo forate to check the really format echo forate检查真正的格式

$askfrmt = date_format($date, 'Y-m-d');

strtotime function on your given date 在给定日期的strtotime功能

$askfrmta = strtotime($askfrmt);
if($thisStart >= $lunchStart && $thisStart <= $lunchEnd)
{
    //occurs during lunch
 } else if ($thisEnd >= $lunchStart && $thisEnd <= $lunchEnd)
{
    //occurs during lunch
}

That should work for you. 这对你有用。

However can you clarify what you mean by it doesn't cover all the times? 但是,你可以澄清你的意思吗?它不会涵盖所有时间?

I'm not realy sure to have understanded your question, but if you want to know if thisStart and thisEnd are both during lunch try this: 我不确定是否已经明确了你的问题,但是如果你想知道thisStartthisEnd都是在午餐期间试试这个:

if (!($thisStart > $lunchEnd || $thisEnd < $lunchStart) ){    
// Occurs during lunch
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM