First all; I'm sorry if my question is not irrelevant. I'm new on array's with PHP.
I have an array;
[
{
"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"
}
]
I want to split time ranges from start to end by hours. Desired output;
[
{
"hour_selected":"08:00:00"
},
{
"hour_selected":"09:00:00"
},
{
"hour_selected":"10:00:00"
},
{
"hour_selected":"11:00:00"
},
{
"hour_selected":"12:00:00"
},
{
"hour_selected":"13:00:00"
}
]
But i'm lost how can i do this with time hour ranges. Any help greatly appricated.
PS: I'm creating array from mysql datetime field.
the simpliest solution is to use unix timestamp:
<?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));
Here you can see working example: http://sandbox.onlinephpfunctions.com/code/8fec4a2f6b067dc66705732b3c43301cc8722d3f
[ { "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" } ]
Apply for each on this and get order_start_time & order_end_time in an array then sort as per values associating as par increasing order of time.