missing date with current days

 <?php

$dateRanges = array(

  array('from' => '2024-04-01', 'to' => '2024-04-05'),

  array('from' => '2024-04-10', 'to' => '2024-04-15'),

  array('from' => '2024-04-20', 'to' => '2024-04-22 '),

  // Add more date ranges as needed

);


function calculateMissingDays($dateRanges) {

  $missingDays = array();


  // Get the maximum date from the date ranges

  $maxDate = null;

  foreach ($dateRanges as $range) {

    $to = new DateTime($range['to']);

    if ($maxDate === null || $to > $maxDate) {

      $maxDate = $to;

    }

  }


  // Get the current date

  $currentDate = new DateTime();


  foreach ($dateRanges as $index => $range) {

    $from = new DateTime($range['from']);

    $to = new DateTime($range['to']);


    if ($index > 0) {

      $prevTo = new DateTime($dateRanges[$index - 1]['to']);

      $diff = $prevTo->diff($from)->days;

      for ($i = 1; $i <= $diff; $i++) { // Changed to <= to exclude 'to' date

        $nextDay = $prevTo->modify('+1 day')->format('Y-m-d');

        // Check if the next day is not already in the date range and is before the current date

        if (!in_array($nextDay, $missingDays) && new DateTime($nextDay) < $currentDate) {

          $missingDays[] = $nextDay;

        }

      }

    }

  }


  // Add missing days up to the current date

  while ($maxDate < $currentDate) {

    $nextDay = $maxDate->format('Y-m-d');

    // Check if the next day is not already in the date range

    if (!in_array($nextDay, $missingDays)) {

      $missingDays[] = $nextDay;

    }

    $maxDate->modify('+1 day');

  }


  return $missingDays;

}


$missingDays = calculateMissingDays($dateRanges);

print_r($missingDays);

?>


Comments

Popular posts from this blog

How to seperate character from string in php

How to Delete record using PHP Ajax

Uploads Only 10 files in month step by step