How to display anything in php in between particular time duration?

后端 未结 7 1889
离开以前
离开以前 2021-01-18 22:05

I have a php code as shown below in which I want to display anything in between two calendar days of the week.

The values coming inside $data->{\"select_s

相关标签:
7条回答
  • 2021-01-18 22:31

    The difficult part of this question lies in handling ranges that 'wrap' around the end of the week, i.e. your example case 5

    I'd suggest setting up a reference array of days that covers two weeks

    $days = ['mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun'];
    $days = array_merge($days, $days);
    

    Slice it so that it starts at the day of the start point (it will be reindexed from 0)

    $days = array_slice($days, array_search($startDay, $days));
    

    You can then build a reference integer for both now and the end point

    $nowRef = (int) (array_search($nowDay, $days) . $nowTime);
    $endRef = (int) (array_search($endDay, $days) . $endTime);
    

    Note that you could do the same for the start point, but as the days array starts with $startDay (index 0) this is equivalent to $startTime

    Your if condition then simply becomes

    if ($nowRef >= $startTime && $nowRef <= $endRef) {
       // IN RANGE
    }
    

    N.B. This assumes that your user inputs have been validated, and that if the start day and end day are the same then the end time is greater than the start time


    Your naming convention is a bit inconsistent, so I have renamed some of your variables and switched to camel case for readability

    $nowDay = $arradate;
    $nowTime = $nowtime;
    
    $startDay = $start_day;
    $startTime = $start_time;
    
    $endDay = $end_day;
    $endTime = $end_time;
    
    0 讨论(0)
提交回复
热议问题