使用 php 打印开始时间和结束时间之间的时间间隔
print time intervals between start time and end time using php
我必须使用 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;
}
请帮帮我,谢谢。
试试这个功能
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;
}
输出为
我想它对你有用。
我必须使用 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;
}
请帮帮我,谢谢。
试试这个功能
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;
}
输出为
我想它对你有用。