D
D
del9937882016-12-05 18:15:29
PHP
del993788, 2016-12-05 18:15:29

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;
    }
}

Where json_decode('[{"datestart":"2016-12-12","dateend":"2016-12-1.... - my database structure
datestart - check-in date dateend - check
-out date
The purpose of the script is to find only those dates that repeat 3 times.For clarity, I made a picture:
a2c40b74090045ab9baef8ae0048c1a2.jpg
only these dates are repeated 3 times: 2016-12-12, 2016-12-14 and 2016-12-16.The
script does not work correctly.It returns, for some reason , only 2 dates: 2016-12-14 and 2016-12-16.2016-12-12
it is losing somewhere.Tell
me where is the error?Where is the first repetition lost?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Artyom Karetnikov, 2016-12-06
@art_karetnikov

"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 question

Ask a Question

731 491 924 answers to any question