3v4l.org

run code in 300+ PHP versions simultaneously
<?php /* This is the result i want from $uptime, all periods where there are no incidents between period_start and period_end. -------------------------------------------------- Array ( [0] => Array ( [start] => 2016-01-01 00:00:00 [end] => 2016-01-05 00:00:00 ) [1] => Array ( [start] => 2016-01-15 22:00:00 [end] => 2016-01-20 00:00:00 ) [2] => Array ( [start] => 2016-01-25 23:59:59 [end] => 2016-01-31 23:59:59 ) ) -------------------------------------------------- */ $period_start = '2016-01-01 00:00:00'; $period_end = '2016-01-31 23:59:59'; $uptime = []; $incidents = [ ['start' => '2016-01-05 00:00:00', 'end' => '2016-01-10 23:59:59'], ['start' => '2016-01-07 00:00:00', 'end' => '2016-01-15 22:00:00'], // overlapping ['start' => '2016-01-08 00:00:00', 'end' => '2016-01-12 23:59:59'], // overlapping ['start' => '2016-01-20 00:00:00', 'end' => '2016-01-25 23:59:59'], ['start' => '2016-01-23 00:00:00', 'end' => '2016-01-24 23:59:59'] // overlapping ]; // First period if(strtotime($period_start) <= strtotime($incidents[0]['start'])) { array_push($uptime, ['start' => $period_start, 'end' => $incidents[0]['start']]); } $last_end = 0; // Used for finding the last ending incident. for($i = 0;$i < sizeof($incidents);$i++) { $push = true; for($j = $i+1;$j < sizeof($incidents);$j++) { if(strtotime($incidents[$i]['end']) < strtotime($incidents[$j]['start'])) { if($push) { array_push($uptime, ['start' => $incidents[$i]['end'], 'end' => $incidents[$j]['start']]); } break; } else { $push = false; } } // Saving for last period if(strtotime($incidents[$i]['end']) > strtotime($last_end)) { $last_end = $incidents[$i]['end']; } } // Last period if(strtotime($period_end) >= strtotime($last_end)) { array_push($uptime, ['start' => $last_end, 'end' => $period_end]); } print_r( $uptime );
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 14, Position 2 = 22
Branch analysis from position: 14
1 jumps found. (Code = 42) Position 1 = 73
Branch analysis from position: 73
2 jumps found. (Code = 44) Position 1 = 76, Position 2 = 25
Branch analysis from position: 76
2 jumps found. (Code = 43) Position 1 = 84, Position 2 = 90
Branch analysis from position: 84
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 90
Branch analysis from position: 25
1 jumps found. (Code = 42) Position 1 = 56
Branch analysis from position: 56
2 jumps found. (Code = 44) Position 1 = 59, Position 2 = 29
Branch analysis from position: 59
2 jumps found. (Code = 43) Position 1 = 69, Position 2 = 72
Branch analysis from position: 69
2 jumps found. (Code = 44) Position 1 = 76, Position 2 = 25
Branch analysis from position: 76
Branch analysis from position: 25
Branch analysis from position: 72
Branch analysis from position: 29
2 jumps found. (Code = 43) Position 1 = 41, Position 2 = 54
Branch analysis from position: 41
2 jumps found. (Code = 43) Position 1 = 42, Position 2 = 52
Branch analysis from position: 42
1 jumps found. (Code = 42) Position 1 = 59
Branch analysis from position: 59
Branch analysis from position: 52
Branch analysis from position: 54
2 jumps found. (Code = 44) Position 1 = 59, Position 2 = 29
Branch analysis from position: 59
Branch analysis from position: 29
Branch analysis from position: 22
filename:       /in/WDGeQ
function name:  (null)
number of ops:  94
compiled vars:  !0 = $period_start, !1 = $period_end, !2 = $uptime, !3 = $incidents, !4 = $last_end, !5 = $i, !6 = $push, !7 = $j
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   34     0  E >   ASSIGN                                                   !0, '2016-01-01+00%3A00%3A00'
   35     1        ASSIGN                                                   !1, '2016-01-31+23%3A59%3A59'
   37     2        ASSIGN                                                   !2, <array>
   39     3        ASSIGN                                                   !3, <array>
   48     4        INIT_FCALL                                               'strtotime'
          5        SEND_VAR                                                 !0
          6        DO_ICALL                                         $12     
          7        INIT_FCALL                                               'strtotime'
          8        FETCH_DIM_R                                      ~13     !3, 0
          9        FETCH_DIM_R                                      ~14     ~13, 'start'
         10        SEND_VAL                                                 ~14
         11        DO_ICALL                                         $15     
         12        IS_SMALLER_OR_EQUAL                                      $12, $15
         13      > JMPZ                                                     ~16, ->22
   49    14    >   INIT_FCALL                                               'array_push'
         15        SEND_REF                                                 !2
         16        INIT_ARRAY                                       ~17     !0, 'start'
         17        FETCH_DIM_R                                      ~18     !3, 0
         18        FETCH_DIM_R                                      ~19     ~18, 'start'
         19        ADD_ARRAY_ELEMENT                                ~17     ~19, 'end'
         20        SEND_VAL                                                 ~17
         21        DO_ICALL                                                 
   52    22    >   ASSIGN                                                   !4, 0
   54    23        ASSIGN                                                   !5, 0
         24      > JMP                                                      ->73
   56    25    >   ASSIGN                                                   !6, <true>
   58    26        ADD                                              ~24     !5, 1
         27        ASSIGN                                                   !7, ~24
         28      > JMP                                                      ->56
   59    29    >   INIT_FCALL                                               'strtotime'
         30        FETCH_DIM_R                                      ~26     !3, !5
         31        FETCH_DIM_R                                      ~27     ~26, 'end'
         32        SEND_VAL                                                 ~27
         33        DO_ICALL                                         $28     
         34        INIT_FCALL                                               'strtotime'
         35        FETCH_DIM_R                                      ~29     !3, !7
         36        FETCH_DIM_R                                      ~30     ~29, 'start'
         37        SEND_VAL                                                 ~30
         38        DO_ICALL                                         $31     
         39        IS_SMALLER                                               $28, $31
         40      > JMPZ                                                     ~32, ->54
   60    41    > > JMPZ                                                     !6, ->52
   61    42    >   INIT_FCALL                                               'array_push'
         43        SEND_REF                                                 !2
         44        FETCH_DIM_R                                      ~33     !3, !5
         45        FETCH_DIM_R                                      ~34     ~33, 'end'
         46        INIT_ARRAY                                       ~35     ~34, 'start'
         47        FETCH_DIM_R                                      ~36     !3, !7
         48        FETCH_DIM_R                                      ~37     ~36, 'start'
         49        ADD_ARRAY_ELEMENT                                ~35     ~37, 'end'
         50        SEND_VAL                                                 ~35
         51        DO_ICALL                                                 
   63    52    > > JMP                                                      ->59
         53*       JMP                                                      ->55
   66    54    >   ASSIGN                                                   !6, <false>
   58    55        PRE_INC                                                  !7
         56    >   COUNT                                            ~41     !3
         57        IS_SMALLER                                               !7, ~41
         58      > JMPNZ                                                    ~42, ->29
   74    59    >   INIT_FCALL                                               'strtotime'
         60        FETCH_DIM_R                                      ~43     !3, !5
         61        FETCH_DIM_R                                      ~44     ~43, 'end'
         62        SEND_VAL                                                 ~44
         63        DO_ICALL                                         $45     
         64        INIT_FCALL                                               'strtotime'
         65        SEND_VAR                                                 !4
         66        DO_ICALL                                         $46     
         67        IS_SMALLER                                               $46, $45
         68      > JMPZ                                                     ~47, ->72
   75    69    >   FETCH_DIM_R                                      ~48     !3, !5
         70        FETCH_DIM_R                                      ~49     ~48, 'end'
         71        ASSIGN                                                   !4, ~49
   54    72    >   PRE_INC                                                  !5
         73    >   COUNT                                            ~52     !3
         74        IS_SMALLER                                               !5, ~52
         75      > JMPNZ                                                    ~53, ->25
   81    76    >   INIT_FCALL                                               'strtotime'
         77        SEND_VAR                                                 !1
         78        DO_ICALL                                         $54     
         79        INIT_FCALL                                               'strtotime'
         80        SEND_VAR                                                 !4
         81        DO_ICALL                                         $55     
         82        IS_SMALLER_OR_EQUAL                                      $55, $54
         83      > JMPZ                                                     ~56, ->90
   82    84    >   INIT_FCALL                                               'array_push'
         85        SEND_REF                                                 !2
         86        INIT_ARRAY                                       ~57     !4, 'start'
         87        ADD_ARRAY_ELEMENT                                ~57     !1, 'end'
         88        SEND_VAL                                                 ~57
         89        DO_ICALL                                                 
   85    90    >   INIT_FCALL                                               'print_r'
         91        SEND_VAR                                                 !2
         92        DO_ICALL                                                 
         93      > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
164.9 ms | 1408 KiB | 19 Q