Domanda

What I need to do is check a "task" does not occur during a lunch hour. I have 4 datetime objects. 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

È stato utile?

Soluzione

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?

Altri suggerimenti

Here you need this logic
Change time to second so it will be easy

but your date should be in ("y/m/d") format

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

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

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

echo forate to check the really format

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

strtotime function on your given date

$askfrmta = strtotime($askfrmt);

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:

if (!($thisStart > $lunchEnd || $thisEnd < $lunchStart) ){    
// Occurs during lunch
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top