首先全部;如果我的问题不相关,我很抱歉。我是PHP的新阵列。
我有一个数组;
CHILD_DATA_SUCCESS
我想按小时从开始到结束分割时间范围。期望的输出;
[
{
"order_id":"7",
"order_start_time":"08:00:00",
"order_end_time":"11:00:00",
"order_date":"29\/03\/2018"
},
{
"order_id":"8",
"order_start_time":"10:00:00",
"order_end_time":"01:00:00",
"order_date":"29\/03\/2018"
}
]
但是我失去了如何用时间范围来做这件事。 任何帮助都很有用。
PS:我正在从mysql datetime字段创建数组。
答案 0 :(得分:1)
最简单的解决方案是使用unix时间戳:
<?php
$timeFrom = '08:00:00';
$timeTo = '15:00:00';
function rangeBetweenHours($from, $to)
{
$timeFrom = strtotime('today ' . $from);
$timeTo = strtotime('today ' . $to);
$out = [];
foreach (range($timeFrom, $timeTo, 60 * 60) as $timestamp) { // 60 * 60 is a hour
$out[] = date('H:i:s', $timestamp);
}
return $out;
}
var_dump(rangeBetweenHours($timeFrom, $timeTo));
在这里你可以看到工作示例: http://sandbox.onlinephpfunctions.com/code/8fec4a2f6b067dc66705732b3c43301cc8722d3f
答案 1 :(得分:0)
[ { “ORDER_ID”: “7”, “order_start_time”: “08:00:00”, “order_end_time”: “11:00:00”, “order_date的”: “29/03/2018” }, { “ORDER_ID”: “8”, “order_start_time”: “10:00:00”, “order_end_time”: “01:00:00”, “order_date的”: “29/03/2018” } ]
在此申请每一项并获得order_start_time&amp;然后,数组中的order_end_time按照与par增加的时间顺序相关联的值进行排序。