fork(1) download
  1. <?php
  2.  
  3. // Datas vindas do banco de dados:
  4. $dates = [
  5. ["2017-05-08 11:28:40", "2017-05-08 17:52:12"],
  6. ["2017-05-08 18:34:02", "2017-05-10 09:02:57"],
  7. ["2017-05-10 09:44:31", "2017-05-10 13:22:10"]
  8. ];
  9.  
  10. // Percorre os intervalos de datas:
  11. foreach ($dates as $index => $date)
  12. {
  13.  
  14. // Cria um objeto DateTime para cada:
  15. $start = new DateTime($date[0]);
  16. $end = new DateTime($date[1]);
  17.  
  18. // Se a diferença entre as datas for superior a um dia:
  19. if ($end->diff($start)->format("%a") > 0)
  20. {
  21. // Lista de fatias do intervalo:
  22. $interval = [];
  23.  
  24. // Enquanto a diferença for maior que um dia:
  25. while ($end->diff($start)->format("%a") > 0)
  26. {
  27. // Define a fatia do intervalo para o dia:
  28.  
  29. $sliceStart = clone $start;
  30. $sliceEnd = clone $start;
  31.  
  32. $interval[] = [$sliceStart->format("Y-m-d H:i:s"), $sliceEnd->setTime(23, 59, 59)->format("Y-m-d H:i:s")];
  33.  
  34. // Incrementa em um dia a data de início:
  35. $start->modify("+1 day")->setTime(0, 0, 0);
  36. }
  37.  
  38. // Define o intervalo para o último dia:
  39. $interval[] = [$start->format("Y-m-d H:i:s"), $end->format("Y-m-d H:i:s")];
  40.  
  41. // Substitui o intervalo original pela lista de fatias:
  42. $dates[$index] = $interval;
  43. }
  44. }
  45.  
  46. print_r($dates);
Success #stdin #stdout 0.01s 83904KB
stdin
Standard input is empty
stdout
Array
(
    [0] => Array
        (
            [0] => 2017-05-08 11:28:40
            [1] => 2017-05-08 17:52:12
        )

    [1] => Array
        (
            [0] => Array
                (
                    [0] => 2017-05-08 18:34:02
                    [1] => 2017-05-08 23:59:59
                )

            [1] => Array
                (
                    [0] => 2017-05-09 00:00:00
                    [1] => 2017-05-09 23:59:59
                )

            [2] => Array
                (
                    [0] => 2017-05-10 00:00:00
                    [1] => 2017-05-10 09:02:57
                )

        )

    [2] => Array
        (
            [0] => 2017-05-10 09:44:31
            [1] => 2017-05-10 13:22:10
        )

)