運用場景: 比如在進行配送時候,需要讓用戶選擇 送達時間, 平台只需要設置每天的營業時間。比如:08:00 到 22:30。 前台在展示時候: 就需要處理成各個時間段來展示:
代碼實現:
<?php /*** * * 步驟一: 時間段 計算出所有間隔 半小時的時間 * 依然范兒特西 */ $time1 = '08:30'; $time2 = '22:00'; $buff = array(); for($i=strtotime($time1) ;$i<=strtotime($time2) ; $i=$i+60*30){ $buff[]=date("H:i",$i); } print_r($buff); /*** * * 步驟二: 組合時間段 * 依然范兒特西 */ $str = array(); $num = count($buff); foreach($buff as $key=>$val){ if($key < $num){ $time_str = strtotime($val); $str[] = array( 'start_time' => $val, 'end_time' =>date('H:i',$time_str +60*30) ); } } print_r($str);
運行結果: 為了截圖,時間范圍寫小了。。實際應用過程中,可根據需求修改