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

报告相同问题?

悬赏问题

  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集