3v4l.org

run code in 300+ 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:25:00", "duration" => "300"); $dates[] = array("date" => "2016-02-18 02:30:00", "duration" => "600"); $alreadyChosenDates[] = array("date" => "2016-02-18 02:10:30", "duration" => "600"); // 10 mins //$alreadyChosenDates[] = array("date" => "2016-02-18 02:05:00", "duration" => "300"); // 5 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]]; } echo "<pre>"; print_r(returnClosestTime($alreadyChosenDates, $dates)); echo "</pre>";
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/RMKqW
function name:  (null)
number of ops:  22
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>
    8    10        ASSIGN_DIM                                               !1
         11        OP_DATA                                                  <array>
   33    12        ECHO                                                     '%3Cpre%3E'
   34    13        INIT_FCALL                                               'print_r'
         14        INIT_FCALL                                               'returnclosesttime'
         15        SEND_VAR                                                 !1
         16        SEND_VAR                                                 !0
         17        DO_FCALL                                      0  $8      
         18        SEND_VAR                                                 $8
         19        DO_ICALL                                                 
   35    20        ECHO                                                     '%3C%2Fpre%3E'
         21      > 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/RMKqW
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
-------------------------------------------------------------------------------------
   11     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   13     2        ASSIGN                                                   !2, <array>
   16     3      > FE_RESET_R                                       $8      !1, ->40
          4    > > FE_FETCH_R                                       ~9      $8, !3, ->40
          5    >   ASSIGN                                                   !4, ~9
   17     6      > FE_RESET_R                                       $11     !0, ->38
          7    > > FE_FETCH_R                                               $11, !5, ->38
   19     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
   20    20        IS_SMALLER                                               !6, 0
         21      > JMPZ                                                     ~20, ->24
         22    >   MUL                                              ~21     !6, -1
         23        ASSIGN                                                   !6, ~21
   23    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
   27    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
   17    37    > > JMP                                                      ->7
         38    >   FE_FREE                                                  $11
   16    39      > JMP                                                      ->4
         40    >   FE_FREE                                                  $8
   30    41        FETCH_DIM_R                                      ~31     !2, 1
         42        FETCH_DIM_R                                      ~32     !1, ~31
         43      > RETURN                                                   ~32
   31    44*     > RETURN                                                   null

End of function returnclosesttime

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
150.42 ms | 1019 KiB | 16 Q