doushi9780 2018-10-12 12:37
浏览 44

PHP使用截止时间选择选项中的当前时间

I have the following script which works great:

<?php
function create_time_range($start, $end, $interval = '30 mins', $format = '12') {
    $startTime = strtotime($start); 
    $endTime   = strtotime($end);
    $returnTimeFormat = ($format == '12')?'g:i:s A':'G:i:s';

    $current   = time(); 
    $addTime   = strtotime('+'.$interval, $current); 
    $diff      = $addTime - $current;

    $times = array(); 
    while ($startTime < $endTime) { 
        $times[] = date($returnTimeFormat, $startTime); 
        $startTime += $diff; 
    } 
    $times[] = date($returnTimeFormat, $startTime); 
    return $times; 
}
$times = create_time_range('7:30', '18:30', '30 mins');
?>

<select name="time_picker">
    <option value="">Select Time</option>
    <?php foreach($times as $key=>$val){ ?>
    <option value="<?php echo $val; ?>"><?php echo $val; ?></option>
    <?php } ?>
</select>

However I would love to have this altered: First of all it should only show time slots available given my current time, second to include a 2 hour window as "cut-of time".

Practical example and ideal outcome: Let's assume it's 1PM, the option select would 1.) not even display the values before 1PM and 2.) only show me the next available slot being 3PM (2 hour window)

I've been struggling with this for a few days but no result at the moment. Some expert advise would be greatly appreciated

http://codepad.org/OT2Qyb00

  • 写回答

1条回答 默认 最新

  • dongqian2021 2018-10-16 11:04
    关注

    You can do it like this:

    <?php
    function create_time_range($start, $end, $interval = '30 mins', $format = '12') {
    $startTime = strtotime($start); 
    $endTime   = strtotime($end);
    $returnTimeFormat = ($format == '12')?'g:i A':'G:i';
    $hour = date('H');
    $minute = (date('i')>30)?'30':'00';
    $current = strtotime("$hour:$minute"); 
    $addTime   = strtotime('+'.$interval, $current); 
    $diff      = $addTime - $current;
    $times = array(); 
    while ($current < $endTime) { 
        $times[] = date($returnTimeFormat, $current) . " " . "$hour:$minute"; 
        $current += $diff; 
    } 
    $times[] = date($returnTimeFormat, $current); 
    return $times; 
    }
    $times = create_time_range('8:00', '18:00', '30 mins');
    ?>
    
    <select name="time_picker">
    <option value="">Select Time</option>
    <?php foreach($times as $key=>$val){ ?>
    <option value="<?php echo $val; ?>"><?php echo $val; ?></option>
    <?php } ?>
    </select>
    

    Here I use the $current as a basetime and I create that one by getting current hour and then time rounded by 30 min and then create time from that.

    I hope this can help ya.

    评论

报告相同问题?

悬赏问题

  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入