3v4l.org

run code in 300+ PHP versions simultaneously
<?php function range_overlap($range1, $range2) { list($start, $end) = explode(' - ', $range1); $start = new DateTime($start); $end = new DateTime($end); $start1 = min($start, $end); $end1 = max($start, $end); list($start, $end) = explode(' - ', $range2); $start = new DateTime($start); $end = new DateTime($end); $start2 = min($start, $end); $end2 = max($start, $end); // check for special cases if ($start1 >= $start2 && $end1 <= $end2) { // range1 completely contained inside range2 $overlap = $start1->diff($end1); } elseif ($start2 >= $start1 && $end2 <= $end1) { // range2 completely contained inside range1 $overlap = $start2->diff($end2); } elseif ($end2 > $end1) { // range1 ends first $overlap = $start2->diff($end1); } else { // range2 ends first $overlap = $start1->diff($end2); } // if overlap is < 0 then there is no overlap $overlap_days = $overlap->invert ? 0 : ($overlap->format('%a') + 1); echo "overlap is $overlap_days days\n"; } range_overlap('2019-01-01 - 2019-01-10', '2019-01-06 - 2019-01-20'); // 5 days range_overlap('2019-01-01 - 2019-03-20', '2019-05-06 - 2019-04-20'); // no overlap range_overlap('2019-01-10 - 2019-05-20', '2019-01-01 - 2019-05-20'); // 131 days range_overlap('2019-01-06 - 2019-01-20', '2019-01-10 - 2019-01-01'); // 5 days range_overlap('2019-01-30 - 2019-01-10', '2019-01-12 - 2019-01-15'); // 4 days range_overlap('2019-02-01 - 2019-03-20', '2019-01-10 - 2019-02-28'); // 28 days
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/0g61b
function name:  (null)
number of ops:  25
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   34     0  E >   INIT_FCALL                                               'range_overlap'
          1        SEND_VAL                                                 '2019-01-01+-+2019-01-10'
          2        SEND_VAL                                                 '2019-01-06+-+2019-01-20'
          3        DO_FCALL                                      0          
   35     4        INIT_FCALL                                               'range_overlap'
          5        SEND_VAL                                                 '2019-01-01+-+2019-03-20'
          6        SEND_VAL                                                 '2019-05-06+-+2019-04-20'
          7        DO_FCALL                                      0          
   36     8        INIT_FCALL                                               'range_overlap'
          9        SEND_VAL                                                 '2019-01-10+-+2019-05-20'
         10        SEND_VAL                                                 '2019-01-01+-+2019-05-20'
         11        DO_FCALL                                      0          
   37    12        INIT_FCALL                                               'range_overlap'
         13        SEND_VAL                                                 '2019-01-06+-+2019-01-20'
         14        SEND_VAL                                                 '2019-01-10+-+2019-01-01'
         15        DO_FCALL                                      0          
   38    16        INIT_FCALL                                               'range_overlap'
         17        SEND_VAL                                                 '2019-01-30+-+2019-01-10'
         18        SEND_VAL                                                 '2019-01-12+-+2019-01-15'
         19        DO_FCALL                                      0          
   39    20        INIT_FCALL                                               'range_overlap'
         21        SEND_VAL                                                 '2019-02-01+-+2019-03-20'
         22        SEND_VAL                                                 '2019-01-10+-+2019-02-28'
         23        DO_FCALL                                      0          
         24      > RETURN                                                   1

Function range_overlap:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 46) Position 1 = 58, Position 2 = 60
Branch analysis from position: 58
2 jumps found. (Code = 43) Position 1 = 61, Position 2 = 66
Branch analysis from position: 61
1 jumps found. (Code = 42) Position 1 = 87
Branch analysis from position: 87
2 jumps found. (Code = 43) Position 1 = 89, Position 2 = 91
Branch analysis from position: 89
1 jumps found. (Code = 42) Position 1 = 96
Branch analysis from position: 96
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 91
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 66
2 jumps found. (Code = 46) Position 1 = 68, Position 2 = 70
Branch analysis from position: 68
2 jumps found. (Code = 43) Position 1 = 71, Position 2 = 76
Branch analysis from position: 71
1 jumps found. (Code = 42) Position 1 = 87
Branch analysis from position: 87
Branch analysis from position: 76
2 jumps found. (Code = 43) Position 1 = 78, Position 2 = 83
Branch analysis from position: 78
1 jumps found. (Code = 42) Position 1 = 87
Branch analysis from position: 87
Branch analysis from position: 83
2 jumps found. (Code = 43) Position 1 = 89, Position 2 = 91
Branch analysis from position: 89
Branch analysis from position: 91
Branch analysis from position: 70
Branch analysis from position: 60
filename:       /in/0g61b
function name:  range_overlap
number of ops:  102
compiled vars:  !0 = $range1, !1 = $range2, !2 = $start, !3 = $end, !4 = $start1, !5 = $end1, !6 = $start2, !7 = $end2, !8 = $overlap, !9 = $overlap_days
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   RECV                                             !0      
          1        RECV                                             !1      
    3     2        INIT_FCALL                                               'explode'
          3        SEND_VAL                                                 '+-+'
          4        SEND_VAR                                                 !0
          5        DO_ICALL                                         $10     
          6        FETCH_LIST_R                                     $11     $10, 0
          7        ASSIGN                                                   !2, $11
          8        FETCH_LIST_R                                     $13     $10, 1
          9        ASSIGN                                                   !3, $13
         10        FREE                                                     $10
    4    11        NEW                                              $15     'DateTime'
         12        SEND_VAR_EX                                              !2
         13        DO_FCALL                                      0          
         14        ASSIGN                                                   !2, $15
    5    15        NEW                                              $18     'DateTime'
         16        SEND_VAR_EX                                              !3
         17        DO_FCALL                                      0          
         18        ASSIGN                                                   !3, $18
    6    19        INIT_FCALL                                               'min'
         20        SEND_VAR                                                 !2
         21        SEND_VAR                                                 !3
         22        DO_ICALL                                         $21     
         23        ASSIGN                                                   !4, $21
    7    24        INIT_FCALL                                               'max'
         25        SEND_VAR                                                 !2
         26        SEND_VAR                                                 !3
         27        DO_ICALL                                         $23     
         28        ASSIGN                                                   !5, $23
    8    29        INIT_FCALL                                               'explode'
         30        SEND_VAL                                                 '+-+'
         31        SEND_VAR                                                 !1
         32        DO_ICALL                                         $25     
         33        FETCH_LIST_R                                     $26     $25, 0
         34        ASSIGN                                                   !2, $26
         35        FETCH_LIST_R                                     $28     $25, 1
         36        ASSIGN                                                   !3, $28
         37        FREE                                                     $25
    9    38        NEW                                              $30     'DateTime'
         39        SEND_VAR_EX                                              !2
         40        DO_FCALL                                      0          
         41        ASSIGN                                                   !2, $30
   10    42        NEW                                              $33     'DateTime'
         43        SEND_VAR_EX                                              !3
         44        DO_FCALL                                      0          
         45        ASSIGN                                                   !3, $33
   11    46        INIT_FCALL                                               'min'
         47        SEND_VAR                                                 !2
         48        SEND_VAR                                                 !3
         49        DO_ICALL                                         $36     
         50        ASSIGN                                                   !6, $36
   12    51        INIT_FCALL                                               'max'
         52        SEND_VAR                                                 !2
         53        SEND_VAR                                                 !3
         54        DO_ICALL                                         $38     
         55        ASSIGN                                                   !7, $38
   14    56        IS_SMALLER_OR_EQUAL                              ~40     !6, !4
         57      > JMPZ_EX                                          ~40     ~40, ->60
         58    >   IS_SMALLER_OR_EQUAL                              ~41     !5, !7
         59        BOOL                                             ~40     ~41
         60    > > JMPZ                                                     ~40, ->66
   16    61    >   INIT_METHOD_CALL                                         !4, 'diff'
         62        SEND_VAR_EX                                              !5
         63        DO_FCALL                                      0  $42     
         64        ASSIGN                                                   !8, $42
   14    65      > JMP                                                      ->87
   18    66    >   IS_SMALLER_OR_EQUAL                              ~44     !4, !6
         67      > JMPZ_EX                                          ~44     ~44, ->70
         68    >   IS_SMALLER_OR_EQUAL                              ~45     !7, !5
         69        BOOL                                             ~44     ~45
         70    > > JMPZ                                                     ~44, ->76
   20    71    >   INIT_METHOD_CALL                                         !6, 'diff'
         72        SEND_VAR_EX                                              !7
         73        DO_FCALL                                      0  $46     
         74        ASSIGN                                                   !8, $46
   18    75      > JMP                                                      ->87
   22    76    >   IS_SMALLER                                               !5, !7
         77      > JMPZ                                                     ~48, ->83
   24    78    >   INIT_METHOD_CALL                                         !6, 'diff'
         79        SEND_VAR_EX                                              !5
         80        DO_FCALL                                      0  $49     
         81        ASSIGN                                                   !8, $49
   22    82      > JMP                                                      ->87
   28    83    >   INIT_METHOD_CALL                                         !4, 'diff'
         84        SEND_VAR_EX                                              !7
         85        DO_FCALL                                      0  $51     
         86        ASSIGN                                                   !8, $51
   31    87    >   FETCH_OBJ_R                                      ~53     !8, 'invert'
         88      > JMPZ                                                     ~53, ->91
         89    >   QM_ASSIGN                                        ~54     0
         90      > JMP                                                      ->96
         91    >   INIT_METHOD_CALL                                         !8, 'format'
         92        SEND_VAL_EX                                              '%25a'
         93        DO_FCALL                                      0  $55     
         94        ADD                                              ~56     $55, 1
         95        QM_ASSIGN                                        ~54     ~56
         96    >   ASSIGN                                                   !9, ~54
   32    97        ROPE_INIT                                     3  ~59     'overlap+is+'
         98        ROPE_ADD                                      1  ~59     ~59, !9
         99        ROPE_END                                      2  ~58     ~59, '+days%0A'
        100        ECHO                                                     ~58
   33   101      > RETURN                                                   null

End of function range_overlap

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
142.15 ms | 1015 KiB | 22 Q