简体   繁体   English

PHP在截止时间选项选择中获取当前时间

[英]PHP get current time in option select with cut-off time

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". 但是,我希望对此进行更改:首先,它应该仅显示给定当前时间的可用时隙,其次应包括2小时窗口作为“截止时间”。

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) 实际示例和理想结果:假设它是1PM,选项select将1.)甚至不显示1PM之前的值; 2。)仅向我显示下一个可用时段为3PM(2小时窗口)

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 http://codepad.org/OT2Qyb00

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. 在这里,我将$ current用作基准时间,并通过获取当前小时数来创建该时间,然后将时间舍入30分钟,然后从中创建时间。

I hope this can help ya. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM