3v4l.org

run code in 500+ PHP versions simultaneously
<?php $dates[] = array("date" => "2016-02-18 02:00:00", "duration" => "600"); // 10 mins $dates[] = array("date" => "2016-02-18 02:05:00", "duration" => "300"); // 5 mins $dates[] = array("date" => "2016-02-18 02:10:00", "duration" => "600"); $dates[] = array("date" => "2016-02-18 02:15:00", "duration" => "300"); $dates[] = array("date" => "2016-02-18 02:20:00", "duration" => "600"); $dates[] = array("date" => "2016-02-18 02:25:00", "duration" => "300"); $dates[] = array("date" => "2016-02-18 02:30:00", "duration" => "600"); $alreadyChosenDates[] = array("date" => "2016-02-18 02:05:00", "duration" => "300"); // 10 mins function returnClosestTime($alreadyChosenDates, $dates){ // Set an array called $closestTime that has the time difference and the key $closestTime = [null, null]; // Check each element in array foreach($dates as $key => $date){ foreach($alreadyChosenDates as $chosenDates){ // Calculate difference between already chosen dates array and the dates array $diff = (strtotime($chosenDates["date"]) + $chosenDates["duration"]) - strtotime($date["date"]); if($diff < 0) $diff = $diff * -1; // If $closestTime is empty, populate it if($closestTime[0] === null) $closestTime = [$diff, $key]; // If $closestTime isn't empty and the current date's time difference // is smaller, populate $closestTime with the time difference and key else if($diff < $closestTime[0]) $closestTime = [$diff, $key]; } } return $dates[$closestTime[1]]; } $alreadyChosenDates[] = returnClosestTime($alreadyChosenDates, $dates); echo "<pre>"; print_r($alreadyChosenDates); echo "</pre>";
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/jYpAE
function name:  (null)
number of ops:  28
compiled vars:  !0 = $dates, !1 = $alreadyChosenDates
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    2     0  E >   ASSIGN_DIM                                                   !0
          1        OP_DATA                                                      <array>
    3     2        ASSIGN_DIM                                                   !0
          3        OP_DATA                                                      <array>
    4     4        ASSIGN_DIM                                                   !0
          5        OP_DATA                                                      <array>
    5     6        ASSIGN_DIM                                                   !0
          7        OP_DATA                                                      <array>
    6     8        ASSIGN_DIM                                                   !0
          9        OP_DATA                                                      <array>
    7    10        ASSIGN_DIM                                                   !0
         11        OP_DATA                                                      <array>
    8    12        ASSIGN_DIM                                                   !0
         13        OP_DATA                                                      <array>
   10    14        ASSIGN_DIM                                                   !1
         15        OP_DATA                                                      <array>
   34    16        INIT_FCALL                                                   'returnclosesttime'
         17        SEND_VAR                                                     !1
         18        SEND_VAR                                                     !0
         19        DO_FCALL                                          0  $11     
         20        ASSIGN_DIM                                                   !1
         21        OP_DATA                                                      $11
   36    22        ECHO                                                         '%3Cpre%3E'
   37    23        INIT_FCALL                                                   'print_r'
         24        SEND_VAR                                                     !1
         25        DO_ICALL                                                     
   38    26        ECHO                                                         '%3C%2Fpre%3E'
         27      > RETURN                                                       1

Function returnclosesttime:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 40
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 40
Branch analysis from position: 5
2 jumps found. (Code = 77) Position 1 = 7, Position 2 = 38
Branch analysis from position: 7
2 jumps found. (Code = 78) Position 1 = 8, Position 2 = 38
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 22, Position 2 = 24
Branch analysis from position: 22
2 jumps found. (Code = 43) Position 1 = 27, Position 2 = 31
Branch analysis from position: 27
1 jumps found. (Code = 42) Position 1 = 37
Branch analysis from position: 37
1 jumps found. (Code = 42) Position 1 = 7
Branch analysis from position: 7
Branch analysis from position: 31
2 jumps found. (Code = 43) Position 1 = 34, Position 2 = 37
Branch analysis from position: 34
1 jumps found. (Code = 42) Position 1 = 7
Branch analysis from position: 7
Branch analysis from position: 37
Branch analysis from position: 24
Branch analysis from position: 38
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 38
Branch analysis from position: 40
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 40
filename:       /in/jYpAE
function name:  returnClosestTime
number of ops:  45
compiled vars:  !0 = $alreadyChosenDates, !1 = $dates, !2 = $closestTime, !3 = $date, !4 = $key, !5 = $chosenDates, !6 = $diff
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   12     0  E >   RECV                                                 !0      
          1        RECV                                                 !1      
   14     2        ASSIGN                                                       !2, <array>
   17     3      > FE_RESET_R                                           $8      !1, ->40
          4    > > FE_FETCH_R                                           ~9      $8, !3, ->40
          5    >   ASSIGN                                                       !4, ~9
   18     6      > FE_RESET_R                                           $11     !0, ->38
          7    > > FE_FETCH_R                                                   $11, !5, ->38
   20     8    >   INIT_FCALL                                                   'strtotime'
          9        FETCH_DIM_R                                          ~12     !5, 'date'
         10        SEND_VAL                                                     ~12
         11        DO_ICALL                                             $13     
         12        FETCH_DIM_R                                          ~14     !5, 'duration'
         13        ADD                                                  ~15     $13, ~14
         14        INIT_FCALL                                                   'strtotime'
         15        FETCH_DIM_R                                          ~16     !3, 'date'
         16        SEND_VAL                                                     ~16
         17        DO_ICALL                                             $17     
         18        SUB                                                  ~18     ~15, $17
         19        ASSIGN                                                       !6, ~18
   21    20        IS_SMALLER                                                   !6, 0
         21      > JMPZ                                                         ~20, ->24
         22    >   MUL                                                  ~21     !6, -1
         23        ASSIGN                                                       !6, ~21
   24    24    >   FETCH_DIM_R                                          ~23     !2, 0
         25        TYPE_CHECK                                        2          ~23
         26      > JMPZ                                                         ~24, ->31
         27    >   INIT_ARRAY                                           ~25     !6
         28        ADD_ARRAY_ELEMENT                                    ~25     !4
         29        ASSIGN                                                       !2, ~25
         30      > JMP                                                          ->37
   28    31    >   FETCH_DIM_R                                          ~27     !2, 0
         32        IS_SMALLER                                                   !6, ~27
         33      > JMPZ                                                         ~28, ->37
         34    >   INIT_ARRAY                                           ~29     !6
         35        ADD_ARRAY_ELEMENT                                    ~29     !4
         36        ASSIGN                                                       !2, ~29
   18    37    > > JMP                                                          ->7
         38    >   FE_FREE                                                      $11
   17    39      > JMP                                                          ->4
         40    >   FE_FREE                                                      $8
   31    41        FETCH_DIM_R                                          ~31     !2, 1
         42        FETCH_DIM_R                                          ~32     !1, ~31
         43      > RETURN                                                       ~32
   32    44*     > RETURN                                                       null

End of function returnclosesttime

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
161.38 ms | 2203 KiB | 16 Q