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; $last_end_j = $incidents[$i]['end']; for($j = $i+1;$j < sizeof($incidents);$j++) { if(strtotime($incidents[$i]['end']) < strtotime($incidents[$j]['start'])) { if(strtotime($incidents[$j]['end']) > strtotime($last_end_j)) { $last_end_j = $incidents[$j]['end']; } if($push) { array_push($uptime, ['start' => $last_end_j, '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 = 87
Branch analysis from position: 87
2 jumps found. (Code = 44) Position 1 = 90, Position 2 = 25
Branch analysis from position: 90
2 jumps found. (Code = 43) Position 1 = 98, Position 2 = 104
Branch analysis from position: 98
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 104
Branch analysis from position: 25
1 jumps found. (Code = 42) Position 1 = 70
Branch analysis from position: 70
2 jumps found. (Code = 44) Position 1 = 73, Position 2 = 32
Branch analysis from position: 73
2 jumps found. (Code = 43) Position 1 = 83, Position 2 = 86
Branch analysis from position: 83
2 jumps found. (Code = 44) Position 1 = 90, Position 2 = 25
Branch analysis from position: 90
Branch analysis from position: 25
Branch analysis from position: 86
Branch analysis from position: 32
2 jumps found. (Code = 43) Position 1 = 44, Position 2 = 68
Branch analysis from position: 44
2 jumps found. (Code = 43) Position 1 = 54, Position 2 = 57
Branch analysis from position: 54
2 jumps found. (Code = 43) Position 1 = 58, Position 2 = 66
Branch analysis from position: 58
1 jumps found. (Code = 42) Position 1 = 73
Branch analysis from position: 73
Branch analysis from position: 66
Branch analysis from position: 57
Branch analysis from position: 68
2 jumps found. (Code = 44) Position 1 = 73, Position 2 = 32
Branch analysis from position: 73
Branch analysis from position: 32
Branch analysis from position: 22
filename:       /in/4ggHf
function name:  (null)
number of ops:  108
compiled vars:  !0 = $period_start, !1 = $period_end, !2 = $uptime, !3 = $incidents, !4 = $last_end, !5 = $i, !6 = $push, !7 = $last_end_j, !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                                                      ->87
   56    25    >   ASSIGN                                                   !6, <true>
   57    26        FETCH_DIM_R                                      ~25     !3, !5
         27        FETCH_DIM_R                                      ~26     ~25, 'end'
         28        ASSIGN                                                   !7, ~26
   59    29        ADD                                              ~28     !5, 1
         30        ASSIGN                                                   !8, ~28
         31      > JMP                                                      ->70
   63    32    >   INIT_FCALL                                               'strtotime'
         33        FETCH_DIM_R                                      ~30     !3, !5
         34        FETCH_DIM_R                                      ~31     ~30, 'end'
         35        SEND_VAL                                                 ~31
         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                                               $32, $35
         43      > JMPZ                                                     ~36, ->68
   65    44    >   INIT_FCALL                                               'strtotime'
         45        FETCH_DIM_R                                      ~37     !3, !8
         46        FETCH_DIM_R                                      ~38     ~37, 'end'
         47        SEND_VAL                                                 ~38
         48        DO_ICALL                                         $39     
         49        INIT_FCALL                                               'strtotime'
         50        SEND_VAR                                                 !7
         51        DO_ICALL                                         $40     
         52        IS_SMALLER                                               $40, $39
         53      > JMPZ                                                     ~41, ->57
   66    54    >   FETCH_DIM_R                                      ~42     !3, !8
         55        FETCH_DIM_R                                      ~43     ~42, 'end'
         56        ASSIGN                                                   !7, ~43
   69    57    > > JMPZ                                                     !6, ->66
   70    58    >   INIT_FCALL                                               'array_push'
         59        SEND_REF                                                 !2
         60        INIT_ARRAY                                       ~45     !7, 'start'
         61        FETCH_DIM_R                                      ~46     !3, !8
         62        FETCH_DIM_R                                      ~47     ~46, 'start'
         63        ADD_ARRAY_ELEMENT                                ~45     ~47, 'end'
         64        SEND_VAL                                                 ~45
         65        DO_ICALL                                                 
   72    66    > > JMP                                                      ->73
         67*       JMP                                                      ->69
   75    68    >   ASSIGN                                                   !6, <false>
   59    69        PRE_INC                                                  !8
         70    >   COUNT                                            ~51     !3
         71        IS_SMALLER                                               !8, ~51
         72      > JMPNZ                                                    ~52, ->32
   83    73    >   INIT_FCALL                                               'strtotime'
         74        FETCH_DIM_R                                      ~53     !3, !5
         75        FETCH_DIM_R                                      ~54     ~53, 'end'
         76        SEND_VAL                                                 ~54
         77        DO_ICALL                                         $55     
         78        INIT_FCALL                                               'strtotime'
         79        SEND_VAR                                                 !4
         80        DO_ICALL                                         $56     
         81        IS_SMALLER                                               $56, $55
         82      > JMPZ                                                     ~57, ->86
   84    83    >   FETCH_DIM_R                                      ~58     !3, !5
         84        FETCH_DIM_R                                      ~59     ~58, 'end'
         85        ASSIGN                                                   !4, ~59
   54    86    >   PRE_INC                                                  !5
         87    >   COUNT                                            ~62     !3
         88        IS_SMALLER                                               !5, ~62
         89      > JMPNZ                                                    ~63, ->25
   90    90    >   INIT_FCALL                                               'strtotime'
         91        SEND_VAR                                                 !1
         92        DO_ICALL                                         $64     
         93        INIT_FCALL                                               'strtotime'
         94        SEND_VAR                                                 !4
         95        DO_ICALL                                         $65     
         96        IS_SMALLER_OR_EQUAL                                      $65, $64
         97      > JMPZ                                                     ~66, ->104
   91    98    >   INIT_FCALL                                               'array_push'
         99        SEND_REF                                                 !2
        100        INIT_ARRAY                                       ~67     !4, 'start'
        101        ADD_ARRAY_ELEMENT                                ~67     !1, 'end'
        102        SEND_VAL                                                 ~67
        103        DO_ICALL                                                 
   94   104    >   INIT_FCALL                                               'print_r'
        105        SEND_VAR                                                 !2
        106        DO_ICALL                                                 
        107      > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
180.35 ms | 1404 KiB | 19 Q