dongwei1895 2014-07-10 08:55
浏览 19
已采纳

PHP日期差异,在数组之间保存每一天

I have an array which each element contains a startdate and enddate formated in 'dd.mm.yy'.

 Array
    (
        [0] =
            (
                [date_start] = 23.07.2014
                [date_end] = 02.08.2014
            )
     )

Now i need to build an array that will contain every day between date_start and date_end to validate them later.

Now my question is how can i calculate every day between those dates and save it to an array ?

  • 写回答

5条回答 默认 最新

  • douzi1991 2014-07-10 09:05
    关注

    This should work:

    $startDate = new DateTime('2014-05-01');
    $endDate  = new DateTime('2014-06-02');
     // calculate diff
    $diff = $startDate->diff($endDate);
    $daysDiff = $diff->days;
    
    
    for ($i = 0; $i < $daysDiff; $i++) {
        // clone so the original date will not be changed
        $currentDate = clone $startDate;
        $currentDate->modify('+'.$i.' days');
        echo $currentDate->format('Y-m-d H:i:s')."
    ";
    }
    

    Now you only have to make this for every array entry and save it to an array

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

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记