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 23:59:59 [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 23:59:59'], // overlapping ['start' => '2016-01-12 00:00:00', 'end' => '2016-01-13 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 uptime 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++) { $uptime_start = $incidents[$i]['end']; $uptime_end = $incidents[$i]['start']; for($j = $i+1;$j < sizeof($incidents);$j++) { // this logic doesnt work as expected! if(strtotime($uptime_start) > strtotime($incidents[$j]['start']) && strtotime($uptime_start) < strtotime($incidents[$j]['end']) && strtotime($uptime_start) < strtotime($incidents[$j+1]['start'])) { $uptime_start = $incidents[$j]['end']; $uptime_end = $incidents[$j+1]['start']; array_push($uptime, ['start' => $uptime_start, 'end' => $uptime_end]); } } // Saving for last period if(strtotime($incidents[$i]['end']) > strtotime($last_end)) { $last_end = $incidents[$i]['end']; } } // Last uptime 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 = 98
Branch analysis from position: 98
2 jumps found. (Code = 44) Position 1 = 101, Position 2 = 25
Branch analysis from position: 101
2 jumps found. (Code = 43) Position 1 = 109, Position 2 = 115
Branch analysis from position: 109
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 115
Branch analysis from position: 25
1 jumps found. (Code = 42) Position 1 = 81
Branch analysis from position: 81
2 jumps found. (Code = 44) Position 1 = 84, Position 2 = 34
Branch analysis from position: 84
2 jumps found. (Code = 43) Position 1 = 94, Position 2 = 97
Branch analysis from position: 94
2 jumps found. (Code = 44) Position 1 = 101, Position 2 = 25
Branch analysis from position: 101
Branch analysis from position: 25
Branch analysis from position: 97
Branch analysis from position: 34
2 jumps found. (Code = 46) Position 1 = 44, Position 2 = 54
Branch analysis from position: 44
2 jumps found. (Code = 46) Position 1 = 55, Position 2 = 66
Branch analysis from position: 55
2 jumps found. (Code = 43) Position 1 = 67, Position 2 = 80
Branch analysis from position: 67
2 jumps found. (Code = 44) Position 1 = 84, Position 2 = 34
Branch analysis from position: 84
Branch analysis from position: 34
Branch analysis from position: 80
Branch analysis from position: 66
Branch analysis from position: 54
Branch analysis from position: 22
filename:       /in/sStTT
function name:  (null)
number of ops:  119
compiled vars:  !0 = $period_start, !1 = $period_end, !2 = $uptime, !3 = $incidents, !4 = $last_end, !5 = $i, !6 = $uptime_start, !7 = $uptime_end, !8 = $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                                         $13     
          7        INIT_FCALL                                               'strtotime'
          8        FETCH_DIM_R                                      ~14     !3, 0
          9        FETCH_DIM_R                                      ~15     ~14, 'start'
         10        SEND_VAL                                                 ~15
         11        DO_ICALL                                         $16     
         12        IS_SMALLER_OR_EQUAL                                      $13, $16
         13      > JMPZ                                                     ~17, ->22
   49    14    >   INIT_FCALL                                               'array_push'
         15        SEND_REF                                                 !2
         16        INIT_ARRAY                                       ~18     !0, 'start'
         17        FETCH_DIM_R                                      ~19     !3, 0
         18        FETCH_DIM_R                                      ~20     ~19, 'start'
         19        ADD_ARRAY_ELEMENT                                ~18     ~20, 'end'
         20        SEND_VAL                                                 ~18
         21        DO_ICALL                                                 
   52    22    >   ASSIGN                                                   !4, 0
   54    23        ASSIGN                                                   !5, 0
         24      > JMP                                                      ->98
   56    25    >   FETCH_DIM_R                                      ~24     !3, !5
         26        FETCH_DIM_R                                      ~25     ~24, 'end'
         27        ASSIGN                                                   !6, ~25
   57    28        FETCH_DIM_R                                      ~27     !3, !5
         29        FETCH_DIM_R                                      ~28     ~27, 'start'
         30        ASSIGN                                                   !7, ~28
   59    31        ADD                                              ~30     !5, 1
         32        ASSIGN                                                   !8, ~30
         33      > JMP                                                      ->81
   62    34    >   INIT_FCALL                                               'strtotime'
         35        SEND_VAR                                                 !6
         36        DO_ICALL                                         $32     
         37        INIT_FCALL                                               'strtotime'
         38        FETCH_DIM_R                                      ~33     !3, !8
         39        FETCH_DIM_R                                      ~34     ~33, 'start'
         40        SEND_VAL                                                 ~34
         41        DO_ICALL                                         $35     
         42        IS_SMALLER                                       ~36     $35, $32
         43      > JMPZ_EX                                          ~36     ~36, ->54
   63    44    >   INIT_FCALL                                               'strtotime'
         45        SEND_VAR                                                 !6
         46        DO_ICALL                                         $37     
         47        INIT_FCALL                                               'strtotime'
         48        FETCH_DIM_R                                      ~38     !3, !8
         49        FETCH_DIM_R                                      ~39     ~38, 'end'
         50        SEND_VAL                                                 ~39
         51        DO_ICALL                                         $40     
         52        IS_SMALLER                                       ~41     $37, $40
         53        BOOL                                             ~36     ~41
         54    > > JMPZ_EX                                          ~36     ~36, ->66
   64    55    >   INIT_FCALL                                               'strtotime'
         56        SEND_VAR                                                 !6
         57        DO_ICALL                                         $42     
         58        INIT_FCALL                                               'strtotime'
         59        ADD                                              ~43     !8, 1
         60        FETCH_DIM_R                                      ~44     !3, ~43
         61        FETCH_DIM_R                                      ~45     ~44, 'start'
         62        SEND_VAL                                                 ~45
         63        DO_ICALL                                         $46     
         64        IS_SMALLER                                       ~47     $42, $46
         65        BOOL                                             ~36     ~47
         66    > > JMPZ                                                     ~36, ->80
   66    67    >   FETCH_DIM_R                                      ~48     !3, !8
         68        FETCH_DIM_R                                      ~49     ~48, 'end'
         69        ASSIGN                                                   !6, ~49
   67    70        ADD                                              ~51     !8, 1
         71        FETCH_DIM_R                                      ~52     !3, ~51
         72        FETCH_DIM_R                                      ~53     ~52, 'start'
         73        ASSIGN                                                   !7, ~53
   68    74        INIT_FCALL                                               'array_push'
         75        SEND_REF                                                 !2
         76        INIT_ARRAY                                       ~55     !6, 'start'
         77        ADD_ARRAY_ELEMENT                                ~55     !7, 'end'
         78        SEND_VAL                                                 ~55
         79        DO_ICALL                                                 
   59    80    >   PRE_INC                                                  !8
         81    >   COUNT                                            ~58     !3
         82        IS_SMALLER                                               !8, ~58
         83      > JMPNZ                                                    ~59, ->34
   75    84    >   INIT_FCALL                                               'strtotime'
         85        FETCH_DIM_R                                      ~60     !3, !5
         86        FETCH_DIM_R                                      ~61     ~60, 'end'
         87        SEND_VAL                                                 ~61
         88        DO_ICALL                                         $62     
         89        INIT_FCALL                                               'strtotime'
         90        SEND_VAR                                                 !4
         91        DO_ICALL                                         $63     
         92        IS_SMALLER                                               $63, $62
         93      > JMPZ                                                     ~64, ->97
   76    94    >   FETCH_DIM_R                                      ~65     !3, !5
         95        FETCH_DIM_R                                      ~66     ~65, 'end'
         96        ASSIGN                                                   !4, ~66
   54    97    >   PRE_INC                                                  !5
         98    >   COUNT                                            ~69     !3
         99        IS_SMALLER                                               !5, ~69
        100      > JMPNZ                                                    ~70, ->25
   82   101    >   INIT_FCALL                                               'strtotime'
        102        SEND_VAR                                                 !1
        103        DO_ICALL                                         $71     
        104        INIT_FCALL                                               'strtotime'
        105        SEND_VAR                                                 !4
        106        DO_ICALL                                         $72     
        107        IS_SMALLER_OR_EQUAL                                      $72, $71
        108      > JMPZ                                                     ~73, ->115
   83   109    >   INIT_FCALL                                               'array_push'
        110        SEND_REF                                                 !2
        111        INIT_ARRAY                                       ~74     !4, 'start'
        112        ADD_ARRAY_ELEMENT                                ~74     !1, 'end'
        113        SEND_VAL                                                 ~74
        114        DO_ICALL                                                 
   86   115    >   INIT_FCALL                                               'print_r'
        116        SEND_VAR                                                 !2
        117        DO_ICALL                                                 
        118      > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
155.48 ms | 1022 KiB | 16 Q