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

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

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.

  • 写回答

1条回答 默认 最新

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

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器