<?php
// Datas vindas do banco de dados:
$dates = [
["2017-05-08 11:28:40", "2017-05-08 17:52:12"],
["2017-05-08 18:34:02", "2017-05-10 09:02:57"],
["2017-05-10 09:44:31", "2017-05-10 13:22:10"]
];
// Percorre os intervalos de datas:
foreach ($dates as $index => $date)
{
// Cria um objeto DateTime para cada:
$start = new DateTime($date[0]);
$end = new DateTime($date[1]);
// Se a diferença entre as datas for superior a um dia:
if ($end->diff($start)->format("%a") > 0)
{
// Lista de fatias do intervalo:
$interval = [];
// Enquanto a diferença for maior que um dia:
while ($end->diff($start)->format("%a") > 0)
{
// Define a fatia do intervalo para o dia:
$sliceStart = clone $start;
$sliceEnd = clone $start;
$interval[] = [$sliceStart->format("Y-m-d H:i:s"), $sliceEnd->setTime(23, 59, 59)->format("Y-m-d H:i:s")];
// Incrementa em um dia a data de início:
$start->modify("+1 day")->setTime(0, 0, 0);
}
// Define o intervalo para o último dia:
$interval[] = [$start->format("Y-m-d H:i:s"), $end->format("Y-m-d H:i:s")];
// Substitui o intervalo original pela lista de fatias:
$dates[$index] = $interval;
}
}
PD9waHAKCi8vIERhdGFzIHZpbmRhcyBkbyBiYW5jbyBkZSBkYWRvczoKJGRhdGVzID0gWwogICAgWyIyMDE3LTA1LTA4IDExOjI4OjQwIiwgIjIwMTctMDUtMDggMTc6NTI6MTIiXSwKICAgIFsiMjAxNy0wNS0wOCAxODozNDowMiIsICIyMDE3LTA1LTEwIDA5OjAyOjU3Il0sCiAgICBbIjIwMTctMDUtMTAgMDk6NDQ6MzEiLCAiMjAxNy0wNS0xMCAxMzoyMjoxMCJdCl07CgovLyBQZXJjb3JyZSBvcyBpbnRlcnZhbG9zIGRlIGRhdGFzOgpmb3JlYWNoICgkZGF0ZXMgYXMgJGluZGV4ID0+ICRkYXRlKQp7CiAgICAKICAgIC8vIENyaWEgdW0gb2JqZXRvIERhdGVUaW1lIHBhcmEgY2FkYToKICAgICRzdGFydCA9IG5ldyBEYXRlVGltZSgkZGF0ZVswXSk7CiAg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