What would be the best way to combine the following strings and determine when the next date would be. The strings are pulled from the database in this format.
<?php
$date = '2020-10-30'; // Current Date
$year = '2020,2021'; // 2 Years
$month = '02,04,07,10'; // 4 Months
$day = '28,29,30'; // 3 Days
// Total of 24 Combos || Excluding Current Date | Next Date Would Be: 2021-02-28
?>
My theory was to combine them as an array and determine the next date from there. However I'm having trouble getting to that point.
The code below was from this question. How to generate in PHP all combinations of items in multiple arrays
<?php
function combinations($arrays, $i = 0) {
if (!isset($arrays[$i])) {
return array();
}
if ($i == count($arrays) - 1) {
return $arrays[$i];
}
// get combinations from subsequent arrays
$tmp = combinations($arrays, $i + 1);
$result = array();
// concat each array from tmp with each element from $arrays[$i]
foreach ($arrays[$i] as $v) {
foreach ($tmp as $t) {
$result[] = is_array($t) ?
array_merge(array($v), $t) :
array($v, $t);
}
}
return $result;
}
print_r(
combinations(
array(
array($day),
array($month),
array($year)
)
)
);
?>
Which outputs:
Array ( [0] => Array ( [0] => 28,29,30 [1] => 02,04,07,10 [2] => 2020,2021 ) )
Next I tried stripping the commas and splitting the strings.
$day = str_replace(",", "", $day);
$day = str_split($day, 2);
$month = str_replace(",", "", $month);
$month = str_split($month, 2);
$year = str_replace(",", "", $year);
$year = str_split($year, 4);
Output:
Array ( [0] => Array ( [0] => Array ( [0] => 28 [1] => 29 [2] => 30 ) [1] => Array ( [0] => 02 [1] => 04 [2] => 07 [3] => 10 ) [2] => 2020 [3] => 2021 ) )
I'm now at a loss as to how I would go about determining what the next date would be. Any advice or pointers would be greatly appreciated.
Thanks