Answer the question
In order to leave comments, you need to log in
Where is the first date lost?
Hello. There is this script:
$diaposons = array_map(
function($e) { return array('datestart' => strtotime($e->datestart), 'dateend' => strtotime($e->dateend)); },
json_decode('[{"datestart":"2016-12-12","dateend":"2016-12-18"},{"datestart":"2016-12-11","dateend":"2016-12-14"},{"datestart":"2016-12-16","dateend":"2016-12-19"},{"datestart":"2016-12-12","dateend":"2016-12-12"},{"datestart":"2016-12-14","dateend":"2016-12-16"}]')
);
usort($diaposons, function($a, $b) { return $a['datestart'] - $b['datestart']; });
$result = [];
$left = $diaposons[0]['datestart'];
$right = $diaposons[0]['dateend'];
$num = count($diaposons);
$day = 24 * 60 * 60;
for ($i = 3; $i < $num; ++$i) {
if ($diaposons[$i]['datestart'] > $right) {
$right = $diaposons[$i]['dateend'];
continue;
}
$end = min($right, $diaposons[$i]['dateend']);
$result[] = array(
'start' => max($left + $day, $diaposons[$i]['datestart']),
'end' => $end
);
$left = $end;
$right = max($right, $diaposons[$i]['dateend']);
}
foreach ($result as $r) {
for ($i = $r['start']; $i <= $r['end']; $i += $day) {
echo date('Y-m-d', $i), PHP_EOL;
}
}
Answer the question
In order to leave comments, you need to log in
"I will not give you a ram, I will give you advice." Here's how to remove it all. To calculate what repeats - use MySQL. It is designed for this and it will not be necessary to fence all these calculations.
select min(m_date), count(*) from tbl_table group by m_date having count(*) >1
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question