我必须使用php打印时间间隔。
我有像
这样的输入$start_time = "09:45:00";
$end_time = "2:45:00";
$interval = "+60 minutes";
im printing array like
$tarray = ['09:45:00', '10:45:00', '11:45:00', '12:45:00', '1:45:00', '2:45:00'];
但我想打印像
这样的数组$result = ['09:45:00-10:45:00', '10:45:00-11:45:00', '11:45:00-12:45:00', '12:45:00-1:45:00', '1:45:00'-'2:45:00'];
从结果数组我也必须分开上午和下午时间。
$am_times = ['09:45:00-10:45:00', '10:45:00-11:45:00', '11:45:00-12:45:00'];
$pm_times = ['12:45:00-1:45:00', '1:45:00'-'2:45:00'];
我试过以下
Illuminate\Support\Collection Object
(
[items:protected] => Array
(
[0] => stdClass Object
(
[id] => 121
[showroom_id] => 22
[showroom_open_day] => Monday
[showroom_start_time] => 01:15:00
[showroom_end_time] => 03:15:00
[created_at] => 2017-07-03 10:43:36
[updated_at] => 2017-07-03 10:43:36
)
[1] => stdClass Object
(
[id] => 122
[showroom_id] => 22
[showroom_open_day] => Tuesday
[showroom_start_time] => 02:15:00
[showroom_end_time] => 22:00:00
[created_at] => 2017-07-03 10:43:36
[updated_at] => 2017-07-03 10:43:36
)
[2] => stdClass Object
(
[id] => 123
[showroom_id] => 22
[showroom_open_day] => Wednesday
[showroom_start_time] => 02:00:00
[showroom_end_time] => 09:00:00
[created_at] => 2017-07-03 10:43:36
[updated_at] => 2017-07-03 10:43:36
)
)
)
foreach ($showroom_timings_result as $st)
{
$start = $st->showroom_start_time;
$end = $st->showroom_end_time;
$interval = '+60 minutes';
$times_array[$st->showroom_open_day] = getTimeSlots($start, $end, $interval);
}
function getTimeSlots($start='00:00:00', $end='23:45:00', $interval='+60 minutes') {
$start_str = strtotime($start);
$end_str = strtotime($end);
$now_str = $start_str;
$data = [];
while($now_str <= $end_str){
$data[] = date('H:i:s', $now_str);
$now_str = strtotime($interval, $now_str);
}
return $data;
}
请帮助我,先谢谢。
答案 0 :(得分:0)
尝试此功能
function getTimeSlots($start = '00:00:00', $end = '23:45:00', $interval = '+15 minutes')
{
$start_str = strtotime($start);
$end_str = strtotime($end);
$now_str = $start_str;
$midTime = strtotime('12:00:00');
$data = [];
$preTime = '';
$index = '';
while ($now_str <= $end_str) {
if ($now_str <= $midTime)
$index = 'AM';
else
$index = 'PM';
if ($preTime) {
$data[$index][] = $preTime . '-' . date('H:i:s', $now_str);
}
$preTime = date('H:i:s', $now_str);
$now_str = strtotime($interval, $now_str);
}
return $data;
}
它产生了输出
我觉得它对你有用。