3v4l.org

run code in 300+ PHP versions simultaneously
<?php function getOverlapDays($ranges) { $overlapDays = 0; // no overlap by default $range = $ranges[0]; // starting with the first element of range array $itemsCnt = count($ranges); for ($id = 1; $id < $itemsCnt; $id++) { $start1 = is_a($range["start"], "DateTime") ? $range["start"] : new DateTime($range["start"]); $end1 = is_a($range["end"], "DateTime") ? $range["end"] : new DateTime($range["end"]); $start2 = is_a($ranges[$id]["start"], "DateTime") ? $ranges[$id]["start"] : new DateTime($ranges[$id]["start"]); $end2 = is_a($ranges[$id]["end"], "DateTime") ? $ranges[$id]["end"] : new DateTime($ranges[$id]["end"]); if ($start1 <= $end2 && $end1 >= $start2) { // if the dates overlap return min($end1, $end2)->diff(max($start2, $start1))->days + 1; // return how many days overlap } else { $range = [ "start" => $start1 > $start2 ? $start1 : $start2, "end" => $end1 > $end2 ? $end1 : $end2 ]; } } return $overlapDays; } // example $ranges = array( array('start' => '2013-12-15', 'end' => '2013-12-31'), array('start' => '2014-01-01', 'end' => '2014-01-31'), array('start' => '2014-01-01', 'end' => '2014-01-01') ); echo getOverlapDays($ranges);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/gRK3K
function name:  (null)
number of ops:  6
compiled vars:  !0 = $ranges
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   28     0  E >   ASSIGN                                                   !0, <array>
   34     1        INIT_FCALL                                               'getoverlapdays'
          2        SEND_VAR                                                 !0
          3        DO_FCALL                                      0  $2      
          4        ECHO                                                     $2
          5      > RETURN                                                   1

Function getoverlapdays:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 112
Branch analysis from position: 112
2 jumps found. (Code = 44) Position 1 = 114, Position 2 = 8
Branch analysis from position: 114
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 14, Position 2 = 17
Branch analysis from position: 14
1 jumps found. (Code = 42) Position 1 = 23
Branch analysis from position: 23
2 jumps found. (Code = 43) Position 1 = 30, Position 2 = 33
Branch analysis from position: 30
1 jumps found. (Code = 42) Position 1 = 39
Branch analysis from position: 39
2 jumps found. (Code = 43) Position 1 = 47, Position 2 = 51
Branch analysis from position: 47
1 jumps found. (Code = 42) Position 1 = 58
Branch analysis from position: 58
2 jumps found. (Code = 43) Position 1 = 66, Position 2 = 70
Branch analysis from position: 66
1 jumps found. (Code = 42) Position 1 = 77
Branch analysis from position: 77
2 jumps found. (Code = 46) Position 1 = 80, Position 2 = 82
Branch analysis from position: 80
2 jumps found. (Code = 43) Position 1 = 83, Position 2 = 98
Branch analysis from position: 83
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 98
2 jumps found. (Code = 43) Position 1 = 100, Position 2 = 102
Branch analysis from position: 100
1 jumps found. (Code = 42) Position 1 = 103
Branch analysis from position: 103
2 jumps found. (Code = 43) Position 1 = 106, Position 2 = 108
Branch analysis from position: 106
1 jumps found. (Code = 42) Position 1 = 109
Branch analysis from position: 109
2 jumps found. (Code = 44) Position 1 = 114, Position 2 = 8
Branch analysis from position: 114
Branch analysis from position: 8
Branch analysis from position: 108
2 jumps found. (Code = 44) Position 1 = 114, Position 2 = 8
Branch analysis from position: 114
Branch analysis from position: 8
Branch analysis from position: 102
2 jumps found. (Code = 43) Position 1 = 106, Position 2 = 108
Branch analysis from position: 106
Branch analysis from position: 108
Branch analysis from position: 82
Branch analysis from position: 70
2 jumps found. (Code = 46) Position 1 = 80, Position 2 = 82
Branch analysis from position: 80
Branch analysis from position: 82
Branch analysis from position: 51
2 jumps found. (Code = 43) Position 1 = 66, Position 2 = 70
Branch analysis from position: 66
Branch analysis from position: 70
Branch analysis from position: 33
2 jumps found. (Code = 43) Position 1 = 47, Position 2 = 51
Branch analysis from position: 47
Branch analysis from position: 51
Branch analysis from position: 17
2 jumps found. (Code = 43) Position 1 = 30, Position 2 = 33
Branch analysis from position: 30
Branch analysis from position: 33
filename:       /in/gRK3K
function name:  getOverlapDays
number of ops:  116
compiled vars:  !0 = $ranges, !1 = $overlapDays, !2 = $range, !3 = $itemsCnt, !4 = $id, !5 = $start1, !6 = $end1, !7 = $start2, !8 = $end2
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
    5     1        ASSIGN                                                   !1, 0
    6     2        FETCH_DIM_R                                      ~10     !0, 0
          3        ASSIGN                                                   !2, ~10
    7     4        COUNT                                            ~12     !0
          5        ASSIGN                                                   !3, ~12
    8     6        ASSIGN                                                   !4, 1
          7      > JMP                                                      ->112
    9     8    >   INIT_FCALL                                               'is_a'
          9        FETCH_DIM_R                                      ~15     !2, 'start'
         10        SEND_VAL                                                 ~15
         11        SEND_VAL                                                 'DateTime'
         12        DO_ICALL                                         $16     
         13      > JMPZ                                                     $16, ->17
         14    >   FETCH_DIM_R                                      ~17     !2, 'start'
         15        QM_ASSIGN                                        ~18     ~17
         16      > JMP                                                      ->23
         17    >   NEW                                              $19     'DateTime'
         18        CHECK_FUNC_ARG                                           
         19        FETCH_DIM_FUNC_ARG                               $20     !2, 'start'
         20        SEND_FUNC_ARG                                            $20
         21        DO_FCALL                                      0          
         22        QM_ASSIGN                                        ~18     $19
         23    >   ASSIGN                                                   !5, ~18
   10    24        INIT_FCALL                                               'is_a'
         25        FETCH_DIM_R                                      ~23     !2, 'end'
         26        SEND_VAL                                                 ~23
         27        SEND_VAL                                                 'DateTime'
         28        DO_ICALL                                         $24     
         29      > JMPZ                                                     $24, ->33
         30    >   FETCH_DIM_R                                      ~25     !2, 'end'
         31        QM_ASSIGN                                        ~26     ~25
         32      > JMP                                                      ->39
         33    >   NEW                                              $27     'DateTime'
         34        CHECK_FUNC_ARG                                           
         35        FETCH_DIM_FUNC_ARG                               $28     !2, 'end'
         36        SEND_FUNC_ARG                                            $28
         37        DO_FCALL                                      0          
         38        QM_ASSIGN                                        ~26     $27
         39    >   ASSIGN                                                   !6, ~26
   11    40        INIT_FCALL                                               'is_a'
         41        FETCH_DIM_R                                      ~31     !0, !4
         42        FETCH_DIM_R                                      ~32     ~31, 'start'
         43        SEND_VAL                                                 ~32
         44        SEND_VAL                                                 'DateTime'
         45        DO_ICALL                                         $33     
         46      > JMPZ                                                     $33, ->51
         47    >   FETCH_DIM_R                                      ~34     !0, !4
         48        FETCH_DIM_R                                      ~35     ~34, 'start'
         49        QM_ASSIGN                                        ~36     ~35
         50      > JMP                                                      ->58
         51    >   NEW                                              $37     'DateTime'
         52        CHECK_FUNC_ARG                                           
         53        FETCH_DIM_FUNC_ARG                               $38     !0, !4
         54        FETCH_DIM_FUNC_ARG                               $39     $38, 'start'
         55        SEND_FUNC_ARG                                            $39
         56        DO_FCALL                                      0          
         57        QM_ASSIGN                                        ~36     $37
         58    >   ASSIGN                                                   !7, ~36
   12    59        INIT_FCALL                                               'is_a'
         60        FETCH_DIM_R                                      ~42     !0, !4
         61        FETCH_DIM_R                                      ~43     ~42, 'end'
         62        SEND_VAL                                                 ~43
         63        SEND_VAL                                                 'DateTime'
         64        DO_ICALL                                         $44     
         65      > JMPZ                                                     $44, ->70
         66    >   FETCH_DIM_R                                      ~45     !0, !4
         67        FETCH_DIM_R                                      ~46     ~45, 'end'
         68        QM_ASSIGN                                        ~47     ~46
         69      > JMP                                                      ->77
         70    >   NEW                                              $48     'DateTime'
         71        CHECK_FUNC_ARG                                           
         72        FETCH_DIM_FUNC_ARG                               $49     !0, !4
         73        FETCH_DIM_FUNC_ARG                               $50     $49, 'end'
         74        SEND_FUNC_ARG                                            $50
         75        DO_FCALL                                      0          
         76        QM_ASSIGN                                        ~47     $48
         77    >   ASSIGN                                                   !8, ~47
   14    78        IS_SMALLER_OR_EQUAL                              ~53     !5, !8
         79      > JMPZ_EX                                          ~53     ~53, ->82
         80    >   IS_SMALLER_OR_EQUAL                              ~54     !7, !6
         81        BOOL                                             ~53     ~54
         82    > > JMPZ                                                     ~53, ->98
   15    83    >   INIT_FCALL                                               'min'
         84        SEND_VAR                                                 !6
         85        SEND_VAR                                                 !8
         86        DO_ICALL                                         $55     
         87        INIT_METHOD_CALL                                         $55, 'diff'
         88        INIT_FCALL                                               'max'
         89        SEND_VAR                                                 !7
         90        SEND_VAR                                                 !5
         91        DO_ICALL                                         $56     
         92        SEND_VAR_NO_REF_EX                                       $56
         93        DO_FCALL                                      0  $57     
         94        FETCH_OBJ_R                                      ~58     $57, 'days'
         95        ADD                                              ~59     ~58, 1
         96      > RETURN                                                   ~59
         97*       JMP                                                      ->111
   18    98    >   IS_SMALLER                                               !7, !5
         99      > JMPZ                                                     ~60, ->102
        100    >   QM_ASSIGN                                        ~61     !5
        101      > JMP                                                      ->103
        102    >   QM_ASSIGN                                        ~61     !7
        103    >   INIT_ARRAY                                       ~62     ~61, 'start'
   19   104        IS_SMALLER                                               !8, !6
        105      > JMPZ                                                     ~63, ->108
        106    >   QM_ASSIGN                                        ~64     !6
        107      > JMP                                                      ->109
        108    >   QM_ASSIGN                                        ~64     !8
        109    >   ADD_ARRAY_ELEMENT                                ~62     ~64, 'end'
   17   110        ASSIGN                                                   !2, ~62
    8   111        PRE_INC                                                  !4
        112    >   IS_SMALLER                                               !4, !3
        113      > JMPNZ                                                    ~67, ->8
   24   114    > > RETURN                                                   !1
   25   115*     > RETURN                                                   null

End of function getoverlapdays

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
163.05 ms | 1411 KiB | 20 Q