duanshan5259
duanshan5259
2019-05-29 20:21
浏览 32
已采纳

需要一个关于如何实现这个问题的逻辑

I want to implement this logic in PHP CodeIgniter.

I have a check-in time options that are in intervals of 30 minutes from 00:00 to 23:30. e.g the time can be 00:00 ,00:30, 01:00, 01:30, 02:00, 02:30 ,03:00, 03:30 ..... 23:30.

Because of 2-hour booking window, the system will display a limited number of check-in time options and as such, a customer cannot see all our check-in time options. It will work based on the current time of the system. The customer will see check-in time options from the nearest check-in time option from his current time till the next 2 hours. For example, the current time is 00:50. The nearest check-in time option is 01:00 and 2 hours from 01:00 is 03:00. Hence, the customer will only see the following check-in time options which will be 5 options at any time of the day; 01:00, 01:30 ,02:00,02:30,03:00 etc

I can't seem to be able to translate this to PHP.

图片转代码服务由CSDN问答提供 功能建议

我想在PHP CodeIgniter中实现这个逻辑。

我有一个 登记入住时间选项,从00:00到23:30,间隔时间为30分钟。 例如,时间可以是00:00,00:30,01:00,01:30,02:00,02:30,03:00,03:30 ..... 23:30。 \ n

由于2小时预订窗口,系统将显示有限数量的登记入住时间选项,因此,客户无法看到我们所有的登记入住时间选项。 它将根据系统的当前时间工作。 客户将在最近的入住时间选项中查看从当前时间到下一个2小时的入住时间选项。 例如,当前时间是00:50。 最近的登记入住时间为01:00,01:00至2:00为2小时。 因此,客户只会看到以下登记入住时间选项,在一天中的任何时间都有5个选项; 01:00,01:30,02:00,02:30,03:00等

我似乎无法将其翻译成PHP。 \ n

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doumeng2637
    doumeng2637 2019-05-29 22:09
    已采纳

    So you need to essentially parse the current time in to hours and minutes, then using a DateInterval go to the nearest (next/forward) half-hour or hour. This becomes option 1. From there it is a matter of generating the next 4 options to equal 2 hours from the first available option. We do this with a for loop and adding on 30m each iteration until all the options are complete.

    <?php
    
    function check_in_options($time) {
    
        $dt = new DateTime($time);
    
        $minutes = $dt->format('i');
        $hour = $dt->format('H');
    
        $interval = $minutes >= 30 ? 60 - $minutes : 30 - $minutes;
        $dt->add(new DateInterval("PT{$interval}M"));
    
        $options[] = $dt->format('H:i');
    
        for ($x = 1; $x <= 4; $x++) {
            $options[] = $dt->add(new DateInterval("PT30M"))->format('H:i');
        }
    
        return $options;
    
    }
    
    $time = '00:50';
    
    $options = check_in_options($time);
    
    echo '<pre>';
    print_r($options);
    

    Result:

    Array
    (
        [0] => 01:00
        [1] => 01:30
        [2] => 02:00
        [3] => 02:30
        [4] => 03:00
    )
    
    点赞 评论

相关推荐