1

I have an array with dates from today -> today + 14 days. Structure like this:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [datum] => DateTimeImmutable Object
                        (
                            [date] => 2019-11-04 16:30:00.000000
                            [timezone_type] => 1
                            [timezone] => +01:00
                        )
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [datum] => DateTimeImmutable Object
                        (
                            [date] => 2019-11-05 17:00:00.000000
                            [timezone_type] => 1
                            [timezone] => +01:00
                        )
                )

        )
)

Now I would like know, which dates are missing.

I try something like this:

for ($i = 0; $i < 14; $i++) {
    $checkDate = DateTime::createFromFormat('Y-m-d H:i', date('Y-m-d H:i'))->add(new DateInterval("P".$i."D"));

    if(array_search($checkDate, array_column($freeTime, 'datum')) !== false) {
        echo "FOUND<br />";
    } else {
        echo "Not Found<br />";
    }
}

But it is only echoing "not found".

Saurav Pathak
  • 796
  • 11
  • 32
Trombone0904
  • 4,132
  • 8
  • 51
  • 104

2 Answers2

1

I saw you didn't manage to find a satisfactory answer yet, maybe this will help you on your way.

/* 
 * step 1: create benchmark array ($benchmarkDates)
 * containing all consecutive dates from $start
 */
$start = '2019-10-27 16:30:00';
$days = 14; // total number of days (including $start)
$dateObj = new DateTimeImmutable($start);
$benchmarkDates[] = $dateObj->format('Y-m-d H:i:s'); // store the first bench date
for ($i = 1; $i < $days; $i++) {
    $period = 'P' . $i . 'D';
    $interval = new DateInterval($period);
    $dateObj0 = $dateObj->add($interval);
    $benchmarkDates[] = $dateObj0->format('Y-m-d H:i:s'); // store the next bench date
}

/* 
 * step 2: retrieve dates from DateTimeImmutable objects
 * in the array you want to check for date-gaps ($subject).
 */
foreach ($subject as $key => $value) {
    foreach ($value as $key => $value) {
        $subjectDates[] =  $value['datum']->format('Y-m-d H:i:s'); // store dates
    }
}

/*
 * step 3: store missing dates
 * check subject against benchmark
 */
foreach($benchmarkDates as $key => $value) {
    if(!in_array($value, $subjectDates)) {
        $missingDates[] = $value;
    }
}

echo '<pre>';
print_r($missingDates);
echo '</pre>';

Working demo

lovelace
  • 1,195
  • 1
  • 7
  • 10
0

$freetime is an array conatining your dates and $start is starting date and $end is ending. Then it will return a missing dates in another array $missingDates.
EDIT 2:
I just splited your multidimensional array and store the date&time object in another array then compare it, Sorry it took so much time. EDIT 3:
$start and $end variables are for checking the dates.

 <?php
    error_reporting(1);
    $freetime     = array();
    $onlydates    = array();
    $missingDates = array();
    for ($i = 0; $i < 14; $i++ ) {
    $freetime[$i][0]['datum'] = DateTime::createFromFormat('Y-m-d H:i', 
    date('Y-m-d H:i'))->add(new DateInterval("P".$i."D"));
    }   
    foreach($freetime as $free){
        foreach($free as $fr){
            foreach($fr as $f){     
              $date = date_format($f,"Y-m-d");
              array_push($onlydates,$date);
            }
        }
   }
   $interval  = new DateInterval('P1D');     
   $Start = date_create("2019-10-27");
   $End   = date_create("2019-11-10");

   $period    = new DatePeriod($Start, $interval, $End);
   foreach($period as $day) {
     $formattedDay = $day->format("Y-m-d");
     if(!in_array($formattedDay, $onlydates)){
        $missingDates[] = $formattedDay;
     }
   }

   foreach($missingDates as $missdates){
      echo " / ".$missdates;
   }


    ?>
Ahmed Ali
  • 1,908
  • 14
  • 28