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 }.
CodePudding user response:
$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
}
CodePudding user response:
Objects of type DateTime have the special property that they can be compared directly.
$datetime1 = new DateTime('03:55:06');//start time
$datetime2 = new DateTime('11:55:06');//end time
$now = new DateTime('now');
if($now >= $datetime1 AND $now < $datetime2){
echo 'Yes';
} else {
echo 'No';
}