3v4l.org

run code in 300+ PHP versions simultaneously
<?php $openTimeslots = ['02-09-2021' => [ '8:00-10:00', '16:00-19:00'], '03-09-2021' => [ '7:00-10:00', '16:15-19:00', '14:00-16:00', '13:00-14:15'], '04-09-2021' => [ '7:15-10:00', '15:15-18:15', '18:30-18:45']]; function timeslotsToTimeline($timeslots) { $timeline = []; foreach ($timeslots as $timeslot) { $intTimes = sscanf(str_replace(':', '', $timeslot), "%d-%d"); $timeline[$intTimes[0]] = $intTimes[1]; } return $timeline; } function timelineToTimeslots($timeline) { $timeslots = []; foreach ($timeline as $start => $finish) { $timeslots[] = intdiv($start, 100) . ':' . substr($start, -2) . '-' . intdiv($finish, 100) . ':' . substr($finish, -2); } return $timeslots; } function intersectTimelines($timeline1, $timeline2) { $newTimeline = []; foreach ($timeline1 as $start1 => $finish1) { foreach ($timeline2 as $start2 => $finish2) { $newStart = max($start1, $start2); $newFinish = min($finish1, $finish2); if ($newStart < $newFinish) { $newTimeline[$newStart] = $newFinish; } } } return $newTimeline; } $commonTimeline = timeslotsToTimeline(array_shift($openTimeslots)); foreach ($openTimeslots as $timeslots) { $newTimeline = timeslotsToTimeline($timeslots); $commonTimeline = intersectTimelines($commonTimeline, $newTimeline); } $commonTimeslots = timelineToTimeslots($commonTimeline); var_export($commonTimeslots);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 9, Position 2 = 20
Branch analysis from position: 9
2 jumps found. (Code = 78) Position 1 = 10, Position 2 = 20
Branch analysis from position: 10
1 jumps found. (Code = 42) Position 1 = 9
Branch analysis from position: 9
Branch analysis from position: 20
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 20
filename:       /in/hEoOh
function name:  (null)
number of ops:  29
compiled vars:  !0 = $openTimeslots, !1 = $commonTimeline, !2 = $timeslots, !3 = $newTimeline, !4 = $commonTimeslots
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   44     1        INIT_FCALL                                               'timeslotstotimeline'
          2        INIT_FCALL                                               'array_shift'
          3        SEND_REF                                                 !0
          4        DO_ICALL                                         $6      
          5        SEND_VAR                                                 $6
          6        DO_FCALL                                      0  $7      
          7        ASSIGN                                                   !1, $7
   46     8      > FE_RESET_R                                       $9      !0, ->20
          9    > > FE_FETCH_R                                               $9, !2, ->20
   47    10    >   INIT_FCALL                                               'timeslotstotimeline'
         11        SEND_VAR                                                 !2
         12        DO_FCALL                                      0  $10     
         13        ASSIGN                                                   !3, $10
   48    14        INIT_FCALL                                               'intersecttimelines'
         15        SEND_VAR                                                 !1
         16        SEND_VAR                                                 !3
         17        DO_FCALL                                      0  $12     
         18        ASSIGN                                                   !1, $12
   46    19      > JMP                                                      ->9
         20    >   FE_FREE                                                  $9
   51    21        INIT_FCALL                                               'timelinetotimeslots'
         22        SEND_VAR                                                 !1
         23        DO_FCALL                                      0  $14     
         24        ASSIGN                                                   !4, $14
   53    25        INIT_FCALL                                               'var_export'
         26        SEND_VAR                                                 !4
         27        DO_ICALL                                                 
         28      > RETURN                                                   1

Function timeslotstotimeline:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 19
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 19
Branch analysis from position: 4
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 19
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 19
filename:       /in/hEoOh
function name:  timeslotsToTimeline
number of ops:  22
compiled vars:  !0 = $timeslots, !1 = $timeline, !2 = $timeslot, !3 = $intTimes
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    9     0  E >   RECV                                             !0      
   11     1        ASSIGN                                                   !1, <array>
   12     2      > FE_RESET_R                                       $5      !0, ->19
          3    > > FE_FETCH_R                                               $5, !2, ->19
   13     4    >   INIT_FCALL                                               'sscanf'
          5        INIT_FCALL                                               'str_replace'
          6        SEND_VAL                                                 '%3A'
          7        SEND_VAL                                                 ''
          8        SEND_VAR                                                 !2
          9        DO_ICALL                                         $6      
         10        SEND_VAR                                                 $6
         11        SEND_VAL                                                 '%25d-%25d'
         12        DO_ICALL                                         $7      
         13        ASSIGN                                                   !3, $7
   14    14        FETCH_DIM_R                                      ~9      !3, 0
         15        FETCH_DIM_R                                      ~11     !3, 1
         16        ASSIGN_DIM                                               !1, ~9
         17        OP_DATA                                                  ~11
   12    18      > JMP                                                      ->3
         19    >   FE_FREE                                                  $5
   16    20      > RETURN                                                   !1
   17    21*     > RETURN                                                   null

End of function timeslotstotimeline

Function timelinetotimeslots:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 30
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 30
Branch analysis from position: 4
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 30
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 30
filename:       /in/hEoOh
function name:  timelineToTimeslots
number of ops:  33
compiled vars:  !0 = $timeline, !1 = $timeslots, !2 = $finish, !3 = $start
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   19     0  E >   RECV                                             !0      
   21     1        ASSIGN                                                   !1, <array>
   22     2      > FE_RESET_R                                       $5      !0, ->30
          3    > > FE_FETCH_R                                       ~6      $5, !2, ->30
          4    >   ASSIGN                                                   !3, ~6
   23     5        INIT_FCALL                                               'intdiv'
          6        SEND_VAR                                                 !3
          7        SEND_VAL                                                 100
          8        DO_ICALL                                         $9      
          9        CONCAT                                           ~10     $9, '%3A'
         10        INIT_FCALL                                               'substr'
         11        SEND_VAR                                                 !3
         12        SEND_VAL                                                 -2
         13        DO_ICALL                                         $11     
         14        CONCAT                                           ~12     ~10, $11
         15        CONCAT                                           ~13     ~12, '-'
   24    16        INIT_FCALL                                               'intdiv'
         17        SEND_VAR                                                 !2
         18        SEND_VAL                                                 100
         19        DO_ICALL                                         $14     
         20        CONCAT                                           ~15     ~13, $14
         21        CONCAT                                           ~16     ~15, '%3A'
         22        INIT_FCALL                                               'substr'
         23        SEND_VAR                                                 !2
         24        SEND_VAL                                                 -2
         25        DO_ICALL                                         $17     
         26        CONCAT                                           ~18     ~16, $17
   23    27        ASSIGN_DIM                                               !1
   24    28        OP_DATA                                                  ~18
   22    29      > JMP                                                      ->3
         30    >   FE_FREE                                                  $5
   26    31      > RETURN                                                   !1
   27    32*     > RETURN                                                   null

End of function timelinetotimeslots

Function intersecttimelines:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 26
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 26
Branch analysis from position: 5
2 jumps found. (Code = 77) Position 1 = 7, Position 2 = 24
Branch analysis from position: 7
2 jumps found. (Code = 78) Position 1 = 8, Position 2 = 24
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 21, Position 2 = 23
Branch analysis from position: 21
1 jumps found. (Code = 42) Position 1 = 7
Branch analysis from position: 7
Branch analysis from position: 23
Branch analysis from position: 24
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 24
Branch analysis from position: 26
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 26
filename:       /in/hEoOh
function name:  intersectTimelines
number of ops:  29
compiled vars:  !0 = $timeline1, !1 = $timeline2, !2 = $newTimeline, !3 = $finish1, !4 = $start1, !5 = $finish2, !6 = $start2, !7 = $newStart, !8 = $newFinish
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   29     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   31     2        ASSIGN                                                   !2, <array>
   32     3      > FE_RESET_R                                       $10     !0, ->26
          4    > > FE_FETCH_R                                       ~11     $10, !3, ->26
          5    >   ASSIGN                                                   !4, ~11
   33     6      > FE_RESET_R                                       $13     !1, ->24
          7    > > FE_FETCH_R                                       ~14     $13, !5, ->24
          8    >   ASSIGN                                                   !6, ~14
   34     9        INIT_FCALL                                               'max'
         10        SEND_VAR                                                 !4
         11        SEND_VAR                                                 !6
         12        DO_ICALL                                         $16     
         13        ASSIGN                                                   !7, $16
   35    14        INIT_FCALL                                               'min'
         15        SEND_VAR                                                 !3
         16        SEND_VAR                                                 !5
         17        DO_ICALL                                         $18     
         18        ASSIGN                                                   !8, $18
   36    19        IS_SMALLER                                               !7, !8
         20      > JMPZ                                                     ~20, ->23
   37    21    >   ASSIGN_DIM                                               !2, !7
         22        OP_DATA                                                  !8
   33    23    > > JMP                                                      ->7
         24    >   FE_FREE                                                  $13
   32    25      > JMP                                                      ->4
         26    >   FE_FREE                                                  $10
   41    27      > RETURN                                                   !2
   42    28*     > RETURN                                                   null

End of function intersecttimelines

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
144.36 ms | 1023 KiB | 25 Q