dongse3348 2017-01-04 05:53
浏览 43
已采纳

php处理业务开放和关闭时间

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

  • 写回答

1条回答 默认 最新

  • doujun7161 2017-01-04 07:05
    关注

    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';        
    
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?