0

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

Kadage
  • 5
  • 2
  • I don't see what this has to do with MySQL – Strawberry Sep 21 '20 at 14:01
  • Ended up being a slim line version of the overall script. The info is pulled from a database, as mentioned in the second line. But you are right, since I didn't end up posting that segment of code, I shall remove the tags for you. ;) – Kadage Sep 21 '20 at 14:06
  • I think that the best way would be transforming the dates in Unix timestamp with `strtotime()` and then compare the strings with simple operators `<=` or `>=`, hope this makes sense. – Marcello Perri Sep 21 '20 at 14:11
  • The end result will be saved as a Unix timestamp, but I'm still not sure the best way to work out all the combos of dates in the strings. – Kadage Sep 21 '20 at 14:20

1 Answers1

0

Don’t build any combinations upfront - just loop through them, until you either find a next date, or determined that there isn’t one.

Loop over the years, inside loop over the months, inside loop over the days.

Concatenate all three values with - in between them, and check if that combination is greater than your input date. If so, set that value as the $next_date, and break out of all three loop levels.

Afterwards, you either have a next date value in $next_date - or still null, which means a next date could not be determined for the given input values.

$date  = '2020-10-30';  // Current Date

$year  = '2020,2021';   // 2 Years
$month = '02,04,07,10'; // 4 Months
$day   = '28,29,30';    // 3 Days

$years = explode(',', $year);
$months = explode(',', $month);
$days = explode(',', $day);

$next_date = null;
foreach($years as $y) {
  foreach($months as $m) {
    foreach($days as $d) {
      $temp = $y.'-'.$m.'-'.$d;
      if($temp > $date) {
        $next_date = $temp;
        break 3;
      }
    }
  }
}
if($next_date) {
  echo 'next date is: ' . $next_date;
}
else {
  echo 'there is no next date in the given range.';
}
04FS
  • 5,660
  • 2
  • 10
  • 21
  • Oh wow! That works perfectly. Thank you so much. Having you as a friend would have saved me a lot of time and a headache! Nothing like what I was thinking... Thanks for explaining it all very well too. Makes perfect sense. – Kadage Sep 21 '20 at 14:28