-1

In php I made an array that looks like this:

array(2) {
  [0]=>
  array(6) {
    ["ID"]=>
    string(1) "1"
    ["Time_start"]=>
    string(8) "10:00:00"
    ["Time_stop"]=>
    string(8) "10:30:00"
    ["Zipcode"]=>
    string(6) "5652AR"
    ["Distance_original"]=>
    string(2) "26"
    ["Distance"]=>
    string(2) "30"
  }
  [1]=>
  array(6) {
    ["ID"]=>
    string(1) "2"
    ["Time_start"]=>
    string(8) "13:00:00"
    ["Time_stop"]=>
    string(8) "13:15:00"
    ["Zipcode"]=>
    string(6) "5702ST"
    ["Distance_original"]=>
    string(2) "12"
    ["Distance"]=>
    string(2) "15"
  }
}

Array looks like this: $klanten[$i]['Time_start'] $klanten[$i]['Time_stop'] etc.

The array will contain more times in the future. Now I generate times in quarter of hours like this:

$times_array = hoursRange( 28800, 75600, 900, 'H:i');
    foreach($times_array as $quarter) {
    echo $quarter.'<br>';
    }

Result: 08:00 08:15 08:30 08:45 09:00 09:15 etc. etc.

What I want to do, is generate the times like above, but without the times that are in "Time_start" and "Time_stop" (and between) in the array. How can I "remove" those times?

I guess i have to do something like this, to compare the times:

$start_time    =   strtotime($Time_start);
$end_time   =   strtotime($Time_stop);

And compare it with $quarter. But i don't know how to check each time with the time in the array (and how to check if it is a time between start- and endtime)

UPDATE: added the function hoursRange (it generates the quarter of hours)

Function hoursRange:

function hoursRange( $lower = 28800, $upper = 75600, $step = 900, $format = '' ) {
    $times = array();

    if ( empty( $format ) ) {
        $format = 'g:i a';
    }

    foreach ( range( $lower, $upper, $step ) as $increment ) {
        $increment = gmdate( 'H:i', $increment );

        list( $hour, $minutes ) = explode( ':', $increment );

        $date = new DateTime( $hour . ':' . $minutes );

        $times[(string) $increment] = $date->format( $format );
    }

    return $times;
}

UPDATE 2:

I have this now:

$times_array = hoursRange( 28800, 75600, 900, 'H:i:s');

foreach($times_array as $quarter) {

    $Time_start = array_column($klanten, 'Time_start');
    $Time_stop = array_column($klanten, 'Time_stop');

    for($i=0; $i < count($Time_start); $i++)
{

$date1 = DateTime::createFromFormat('H:i:s', $quarter);
$date2 = DateTime::createFromFormat('H:i:s', $Time_start[$i]);
$date3 = DateTime::createFromFormat('H:i:s', $Time_stop[$i]);
if ($date1 < $date2 && $date1 > $date3)
{
   //echo $quarter;
}
else{
    echo $quarter.'<br>';
}




}

Result: It generates all times two times now.

  • The function `hoursRange()`, what does that thing do ? It does not seems to be standard PHP, and I would not like to guess. – Luuk Dec 29 '22 at 15:22
  • I updatet the openingpost with the function. It generates the quarters of hours. – Patrick_iot Dec 29 '22 at 15:27
  • [How to check if time is between two times in PHP](https://stackoverflow.com/questions/15911312/how-to-check-if-time-is-between-two-times-in-php) – Luuk Dec 29 '22 at 15:32
  • I've updatet my startpost. It generates all times two times now. – Patrick_iot Dec 29 '22 at 15:58
  • To form a [mcve], remove all irrelevant data from your input array. If we don't need to see the zipcode or distance columns, omit them from your question. We prefer `var_export()` text when presenting array data so that we can instantly copy the text into our preferred sandbix environment. We also need to know if your time expressions always have 2-digit hours (zero-padded). – mickmackusa Dec 29 '22 at 20:42
  • Thank you, good to know! I solved the problem for now, but next time I will do this. – Patrick_iot Dec 30 '22 at 08:45

1 Answers1

1

I solved the problem myself, what I did:

  1. First filter all times used in the array and put them in a new array

  2. Then use array_diff on the array with al the times (quarters) and the array with the times that are used

  3. Now I have an array with only the times available

    $times_array = hoursRange( 28800, 75600, 900, 'H:i:s');
    $used_times = array();
    foreach($times_array as $quarter) {
    
    
    
     $Time_start = array_column($klanten, 'Time_start');
     $Time_stop = array_column($klanten, 'Time_stop');
    
    
    for($i=0; $i < count($Time_start); $i++) 
    {
       $quart_time[$i]    =   strtotime($quarter);
       $start_time[$i]   =   strtotime($Time_start[$i]);
       $stop_time[$i]   =   strtotime($Time_stop[$i]);
    
      if ($quart_time[$i] >= $start_time[$i] && $quart_time[$i] <= $stop_time[$i])
         {
    
             array_push($used_times, $quarter);
    
         }
    
     }
    }
    $times_available = array_diff($times_array, $used_times);
    
    foreach($times_available as $available_times) {
        echo $available_times.'<br>';
    }