Xiao Xinqi
Xiao Xinqi

Reputation: 521

php handle business open and close time

Try to get each store open and close condition, if in operation time echo open, if not echo close.

for example: monday-friday: 11:00 am - 2:00 am (which open to midnight)

here is my code

$currentTime = strtotime("01:14 pm");       $rangeStart = strtotime("11:00am");         $rangeEnd = strtotime("tomorrow 02:00 am");
if ($currentTime >= $rangeStart && $currentTime <= $rangeEnd) {
   echo 'in range';         
} else {
        echo 'not in range';        
}

this working good, because current time is 01:14 pm afternoon.

so i try to set the current time to 01:14 am midnight, here is the problem.

which will echo not in range, because $currentTime is less than $rangeStart.

this problem alrady bother me for 2 days.....any help please, thanks

Upvotes: 0

Views: 232

Answers (1)

Amruth ls
Amruth ls

Reputation: 64

Check your $currentTime is less than $rangeStart.

If true then increase $currentTime by one day.

<?php
$currentTime = date('d-m-Y H:i', strtotime("01:14 am"));      
$rangeStart = date('d-m-Y H:i', strtotime("11:00 am"));         
$rangeEnd = date('d-m-Y H:i', strtotime("tomorrow 02:00 am"));

if($currentTime < $rangeStart){
    $currentTime = date('d-m-Y H:i', strtotime("tomorrow".date('H:i',strtotime($currentTime))));     
}


if ($currentTime >= $rangeStart && $currentTime <= $rangeEnd) {
    echo 'in range';         
} else {

    echo 'not in range';        

}
?>

Upvotes: 3

Related Questions