Skip to content
Advertisement

I need to check if time now is between two times

I need to check IF time is between two times with PHP I tried a lot of examples but they are not working for me, I guess I’m asking a stupid question but I’m not finding any answer.

$datetime1 = new DateTime('03:55:06');//start time
$datetime2 = new DateTime('11:55:06');//end time

I need to check IF current time is between $datetime1 and $datetime2.

IF is between { do something } ELSE { do somehting }.

Advertisement

Answer

$start = new DateTime("11:59:59");
$end = new DateTime("13:59:59");
$now = new DateTime();

if($now < $end and $now > $start)
{
    echo "Yes, now is between start and end";
}
else 
{
  // do something else
}
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement