3v4l.org

run code in 300+ PHP versions simultaneously
<?php $dates = array("2012-06-11 08:30:49", "2012-06-07 08:03:54", "2012-05-26 23:04:04", "2012-05-27 08:30:00", "2012-06-08 08:30:55", "2012-06-12 07:45:45"); $children = array("callum" => "1028", "daniel" => "1029"); $closeDate = array(); $firstDate = strtotime("2012-05-29 07:45:45"); function returnClosestDate($firstTime, $dates, $child){ global $initialLoop; global $closeDate; foreach ($dates as $key => $date) { $dateToCompare = strtotime($date); $diff = $firstTime - $dateToCompare; if ($diff < 0) $diff *= -1; if (count($closeDate) == 0) { $closeDate[][$child] = array("date" => $date, "diff" => $diff); continue; } for ($x = 0; $x <= count($closeDate); $x++) { if(isset($closeDate[$x])){ if ($diff < $closeDate[$x][$child]["diff"]) { $closeDate[$x][$child] = array("date" => $date, "diff" => $diff); } } } //if(!alreadyExistInArray($closeDate, $date, $child)){ //if ($diff < $closeDate[$key-1]["diff"]) { //$closeDate[$key] = array("child" => $child, "date" => $date, "diff" => $diff); // } //} } echo "<pre>"; print_r($closeDate); echo "</pre>"; exit; } function alreadyExistInArray($closeDate, $date, $child){ foreach($closeDate as $key => $close){ if($close["child"] === $child) return true; } } foreach($children as $key => $child){ returnClosestDate($firstDate, $dates, $child); }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 8, Position 2 = 16
Branch analysis from position: 8
2 jumps found. (Code = 78) Position 1 = 9, Position 2 = 16
Branch analysis from position: 9
1 jumps found. (Code = 42) Position 1 = 8
Branch analysis from position: 8
Branch analysis from position: 16
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 16
filename:       /in/0h7HZ
function name:  (null)
number of ops:  18
compiled vars:  !0 = $dates, !1 = $children, !2 = $closeDate, !3 = $firstDate, !4 = $child, !5 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   ASSIGN                                                   !0, <array>
    5     1        ASSIGN                                                   !1, <array>
    6     2        ASSIGN                                                   !2, <array>
    7     3        INIT_FCALL                                               'strtotime'
          4        SEND_VAL                                                 '2012-05-29+07%3A45%3A45'
          5        DO_ICALL                                         $9      
          6        ASSIGN                                                   !3, $9
   48     7      > FE_RESET_R                                       $11     !1, ->16
          8    > > FE_FETCH_R                                       ~12     $11, !4, ->16
          9    >   ASSIGN                                                   !5, ~12
   49    10        INIT_FCALL                                               'returnclosestdate'
         11        SEND_VAR                                                 !3
         12        SEND_VAR                                                 !0
         13        SEND_VAR                                                 !4
         14        DO_FCALL                                      0          
   48    15      > JMP                                                      ->8
         16    >   FE_FREE                                                  $11
   50    17      > RETURN                                                   1

Function returnclosestdate:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 6, Position 2 = 45
Branch analysis from position: 6
2 jumps found. (Code = 78) Position 1 = 7, Position 2 = 45
Branch analysis from position: 7
2 jumps found. (Code = 43) Position 1 = 16, Position 2 = 17
Branch analysis from position: 16
2 jumps found. (Code = 43) Position 1 = 20, Position 2 = 26
Branch analysis from position: 20
1 jumps found. (Code = 42) Position 1 = 6
Branch analysis from position: 6
Branch analysis from position: 26
1 jumps found. (Code = 42) Position 1 = 41
Branch analysis from position: 41
2 jumps found. (Code = 44) Position 1 = 44, Position 2 = 28
Branch analysis from position: 44
1 jumps found. (Code = 42) Position 1 = 6
Branch analysis from position: 6
Branch analysis from position: 28
2 jumps found. (Code = 43) Position 1 = 30, Position 2 = 40
Branch analysis from position: 30
2 jumps found. (Code = 43) Position 1 = 35, Position 2 = 40
Branch analysis from position: 35
2 jumps found. (Code = 44) Position 1 = 44, Position 2 = 28
Branch analysis from position: 44
Branch analysis from position: 28
Branch analysis from position: 40
Branch analysis from position: 40
Branch analysis from position: 17
Branch analysis from position: 45
1 jumps found. (Code = 79) Position 1 = -2
Branch analysis from position: 45
filename:       /in/0h7HZ
function name:  returnClosestDate
number of ops:  53
compiled vars:  !0 = $firstTime, !1 = $dates, !2 = $child, !3 = $initialLoop, !4 = $closeDate, !5 = $date, !6 = $key, !7 = $dateToCompare, !8 = $diff, !9 = $x
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    9     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
   10     3        BIND_GLOBAL                                              !3, 'initialLoop'
   11     4        BIND_GLOBAL                                              !4, 'closeDate'
   12     5      > FE_RESET_R                                       $10     !1, ->45
          6    > > FE_FETCH_R                                       ~11     $10, !5, ->45
          7    >   ASSIGN                                                   !6, ~11
   14     8        INIT_FCALL                                               'strtotime'
          9        SEND_VAR                                                 !5
         10        DO_ICALL                                         $13     
         11        ASSIGN                                                   !7, $13
   15    12        SUB                                              ~15     !0, !7
         13        ASSIGN                                                   !8, ~15
   16    14        IS_SMALLER                                               !8, 0
         15      > JMPZ                                                     ~17, ->17
         16    >   ASSIGN_OP                                     3          !8, -1
   18    17    >   COUNT                                            ~19     !4
         18        IS_EQUAL                                                 ~19, 0
         19      > JMPZ                                                     ~20, ->26
   19    20    >   INIT_ARRAY                                       ~23     !5, 'date'
         21        ADD_ARRAY_ELEMENT                                ~23     !8, 'diff'
         22        FETCH_DIM_W                                      $21     !4
         23        ASSIGN_DIM                                               $21, !2
         24        OP_DATA                                                  ~23
   20    25      > JMP                                                      ->6
   23    26    >   ASSIGN                                                   !9, 0
         27      > JMP                                                      ->41
   24    28    >   ISSET_ISEMPTY_DIM_OBJ                         0          !4, !9
         29      > JMPZ                                                     ~25, ->40
   25    30    >   FETCH_DIM_R                                      ~26     !4, !9
         31        FETCH_DIM_R                                      ~27     ~26, !2
         32        FETCH_DIM_R                                      ~28     ~27, 'diff'
         33        IS_SMALLER                                               !8, ~28
         34      > JMPZ                                                     ~29, ->40
   26    35    >   INIT_ARRAY                                       ~32     !5, 'date'
         36        ADD_ARRAY_ELEMENT                                ~32     !8, 'diff'
         37        FETCH_DIM_W                                      $30     !4, !9
         38        ASSIGN_DIM                                               $30, !2
         39        OP_DATA                                                  ~32
   23    40    >   PRE_INC                                                  !9
         41    >   COUNT                                            ~34     !4
         42        IS_SMALLER_OR_EQUAL                                      !9, ~34
         43      > JMPNZ                                                    ~35, ->28
   12    44    > > JMP                                                      ->6
         45    >   FE_FREE                                                  $10
   36    46        ECHO                                                     '%3Cpre%3E'
   37    47        INIT_FCALL                                               'print_r'
         48        SEND_VAR                                                 !4
         49        DO_ICALL                                                 
   38    50        ECHO                                                     '%3C%2Fpre%3E'
   39    51      > EXIT                                                     
   40    52*     > RETURN                                                   null

End of function returnclosestdate

Function alreadyexistinarray:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 12
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 12
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 11
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 11
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 12
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
filename:       /in/0h7HZ
function name:  alreadyExistInArray
number of ops:  14
compiled vars:  !0 = $closeDate, !1 = $date, !2 = $child, !3 = $close, !4 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   42     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
   43     3      > FE_RESET_R                                       $5      !0, ->12
          4    > > FE_FETCH_R                                       ~6      $5, !3, ->12
          5    >   ASSIGN                                                   !4, ~6
   44     6        FETCH_DIM_R                                      ~8      !3, 'child'
          7        IS_IDENTICAL                                             !2, ~8
          8      > JMPZ                                                     ~9, ->11
          9    >   FE_FREE                                                  $5
         10      > RETURN                                                   <true>
   43    11    > > JMP                                                      ->4
         12    >   FE_FREE                                                  $5
   46    13      > RETURN                                                   null

End of function alreadyexistinarray

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
184.61 ms | 952 KiB | 19 Q