$startDate = May 1
$endDate = December 31
[
'before' => 'May 1, September 31',
'match' => 'September 1, October 31',
'after' => 'October 31, December 31',
]
$startDate = October 14
$endDate = December 15
[
'before' => null,
'match' => 'October 14, October 31',
'after' => 'October 31, December 15',
]
$startDate = November 14
$endDate = December 12
[
'before' => null,
'match' => null,
'after' => 'November 14, December 12',
]
.
начало
---|---|---
к | 1
о -
н | 2 1
е -
ц | 3 2 1
if - else if - else
function ranges(int $a, int $b): array
{
$start = 20;
$finish = 30;
$result = [
'before' => null,
'match' => null,
'after' => null,
];
if ($a < $start) {
if ($b < $start) {
$result['before'] = [$a, $b];
} else {
$result['before'] = [$a, $start];
if ($b < $finish) {
$result['match'] = [$start, $b];
} else {
$result['match'] = [$start, $finish];
$result['after'] = [$finish, $b];
}
}
} else if ($a < $finish) {
if ($b < $finish) {
$result['match'] = [$a, $b];
} else {
$result['match'] = [$a, $finish];
$result['after'] = [$finish, $b];
}
} else {
$result['after'] = [$a, $b];
}
return $result;
}
print(json_encode(ranges(5, 25))); // {"before":[5,20],"match":[20,25],"after":null}
$arr = [$start, $finish, $a, $b];
sort($arr, SORT_NUMERIC);