首页 > 代码库 > 生成时间下拉菜单【有待完善】

生成时间下拉菜单【有待完善】

<?php    header(‘content-type:text/html;charset="utf-8"‘);    date_default_timezone_set(‘PRC‘); //设置中国时区         $amstart = ‘17:00‘;        //AM            $amend = ‘18:00‘;        //PM    $pmstart = ‘18:30‘;    $pmend = ‘19:30‘;        function getoption($amstart,$amend,$pmstart,$pmend){        $amstart = strtotime($amstart);        $amend = strtotime($amend);        $pmstart = strtotime($pmstart);        $pmend = strtotime($pmend);        //$dateTime = strtotime(date(‘H:i‘));        $am = array();                //计算上午时间范围差        $AmTime = floor(($amend - $amstart)/3600);        //计算下午时间范围差        $PmTime =  floor(($pmend - $pmstart)/3600);        $apTime = ($amend - $pmstart)/3600;        //时间间隔        $time = 30 * 60;        //echo "开始 - 结束: ".change($amstart)." --- ".change($amend);        //echo "<br>开始 - 结束: ".change($pmstart)." --- ".change($pmend);        //echo "<br>上午时间差: ".$AmTime,"<br>下午时间差: ".$PmTime;      
     for($i=1;$i<=$AmTime*2-1;$i++){ if($i == 1){ $am[$i][‘b‘] = $amstart + $time; //开始时间 $am[$i][‘e‘] = $am[$i][‘b‘] + $time; //结束时间 }else{ $am[$i][‘b‘] = $am[$i-1][‘e‘]; $am[$i][‘e‘] = $am[$i][‘b‘] + $time; } } //var_dump(count($am)+1); $am[count($am)+1][‘d‘] = 1; $am[count($am)][‘e‘] = $am[count($am)-1][‘e‘]; for($j=1;$j<=$PmTime*2-1;$j++){ if($j == 1){ $pm[$j][‘b‘] = $pmstart + $time; //开始时间 $pm[$j][‘e‘] = $pm[$j][‘b‘] + $time; //结束时间 }else{ $pm[$j][‘b‘] = $pm[$j-1][‘e‘]; $pm[$j][‘e‘] = $pm[$j][‘b‘] + $time; } $pm[$j][‘d‘] = 0; }
$pm[count($pm)+1][‘d‘] = 1; $pm[count($pm)][‘e‘] = $pm[count($pm)-1][‘e‘];
    
//合并上下时间 $TimeList = array_merge($am , $pm); echo "<hr>"; $str = "\n\r"; $k = 1; foreach($TimeList as $v){ //d($v[‘b‘]); if($k == 5){ return $str; } if($v[‘d‘] == 1 && time()<=$v[‘e‘]){ $str .= "<option>当前时间为下班时间,不能送餐!</option>"; $k++; } // if($v[‘d‘] == 2 && time()<=$v[‘e‘]){ // $str .= "<option>当前时间为下班时间,不能送餐!</option>"; // $k++; // } if($v[‘b‘] > time() ){ $str .= "<option>".change($v[‘b‘]).‘--‘.change($v[‘e‘])."</option>"; $k ++; } } return $str; } $dat = getoption($amstart,$amend,$pmstart,$pmend); d($dat); echo ‘<select name="some">‘; echo $dat; echo ‘</sclect>‘; function d($v){ echo ‘<pre>‘; var_dump($v); echo ‘</pre>‘; } function change($date){ //时间戳转换成时分秒 return date(‘H:i‘,$date); }?>

大家帮忙看看提出点意见!谢谢!!!