dshqd84261 2018-03-28 07:40
浏览 96
已采纳

php拆分合并新数组的开始时间和结束时间

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.

  • 写回答

2条回答 默认 最新

  • doufen3134 2018-03-28 07:54
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决