3v4l.org

run code in 500+ 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 = 46, Position 2 = 48
Branch analysis from position: 46
2 jumps found. (Code = 43) Position 1 = 49, Position 2 = 54
Branch analysis from position: 49
1 jumps found. (Code = 42) Position 1 = 75
Branch analysis from position: 75
2 jumps found. (Code = 43) Position 1 = 77, Position 2 = 79
Branch analysis from position: 77
1 jumps found. (Code = 42) Position 1 = 84
Branch analysis from position: 84
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 79
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 54
2 jumps found. (Code = 46) Position 1 = 56, Position 2 = 58
Branch analysis from position: 56
2 jumps found. (Code = 43) Position 1 = 59, Position 2 = 64
Branch analysis from position: 59
1 jumps found. (Code = 42) Position 1 = 75
Branch analysis from position: 75
Branch analysis from position: 64
2 jumps found. (Code = 43) Position 1 = 66, Position 2 = 71
Branch analysis from position: 66
1 jumps found. (Code = 42) Position 1 = 75
Branch analysis from position: 75
Branch analysis from position: 71
2 jumps found. (Code = 43) Position 1 = 77, Position 2 = 79
Branch analysis from position: 77
Branch analysis from position: 79
Branch analysis from position: 58
Branch analysis from position: 48
filename:       /in/0g61b
function name:  range_overlap
number of ops:  90
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        FRAMELESS_ICALL_2                min                 ~21     !2, !3
         20        ASSIGN                                                       !4, ~21
    7    21        FRAMELESS_ICALL_2                max                 ~23     !2, !3
         22        ASSIGN                                                       !5, ~23
    8    23        INIT_FCALL                                                   'explode'
         24        SEND_VAL                                                     '+-+'
         25        SEND_VAR                                                     !1
         26        DO_ICALL                                             $25     
         27        FETCH_LIST_R                                         $26     $25, 0
         28        ASSIGN                                                       !2, $26
         29        FETCH_LIST_R                                         $28     $25, 1
         30        ASSIGN                                                       !3, $28
         31        FREE                                                         $25
    9    32        NEW                                                  $30     'DateTime'
         33        SEND_VAR_EX                                                  !2
         34        DO_FCALL                                          0          
         35        ASSIGN                                                       !2, $30
   10    36        NEW                                                  $33     'DateTime'
         37        SEND_VAR_EX                                                  !3
         38        DO_FCALL                                          0          
         39        ASSIGN                                                       !3, $33
   11    40        FRAMELESS_ICALL_2                min                 ~36     !2, !3
         41        ASSIGN                                                       !6, ~36
   12    42        FRAMELESS_ICALL_2                max                 ~38     !2, !3
         43        ASSIGN                                                       !7, ~38
   14    44        IS_SMALLER_OR_EQUAL                                  ~40     !6, !4
         45      > JMPZ_EX                                              ~40     ~40, ->48
         46    >   IS_SMALLER_OR_EQUAL                                  ~41     !5, !7
         47        BOOL                                                 ~40     ~41
         48    > > JMPZ                                                         ~40, ->54
   16    49    >   INIT_METHOD_CALL                                             !4, 'diff'
         50        SEND_VAR_EX                                                  !5
         51        DO_FCALL                                          0  $42     
         52        ASSIGN                                                       !8, $42
   14    53      > JMP                                                          ->75
   18    54    >   IS_SMALLER_OR_EQUAL                                  ~44     !4, !6
         55      > JMPZ_EX                                              ~44     ~44, ->58
         56    >   IS_SMALLER_OR_EQUAL                                  ~45     !7, !5
         57        BOOL                                                 ~44     ~45
         58    > > JMPZ                                                         ~44, ->64
   20    59    >   INIT_METHOD_CALL                                             !6, 'diff'
         60        SEND_VAR_EX                                                  !7
         61        DO_FCALL                                          0  $46     
         62        ASSIGN                                                       !8, $46
   18    63      > JMP                                                          ->75
   22    64    >   IS_SMALLER                                                   !5, !7
         65      > JMPZ                                                         ~48, ->71
   24    66    >   INIT_METHOD_CALL                                             !6, 'diff'
         67        SEND_VAR_EX                                                  !5
         68        DO_FCALL                                          0  $49     
         69        ASSIGN                                                       !8, $49
   22    70      > JMP                                                          ->75
   28    71    >   INIT_METHOD_CALL                                             !4, 'diff'
         72        SEND_VAR_EX                                                  !7
         73        DO_FCALL                                          0  $51     
         74        ASSIGN                                                       !8, $51
   31    75    >   FETCH_OBJ_R                                          ~53     !8, 'invert'
         76      > JMPZ                                                         ~53, ->79
         77    >   QM_ASSIGN                                            ~54     0
         78      > JMP                                                          ->84
         79    >   INIT_METHOD_CALL                                             !8, 'format'
         80        SEND_VAL_EX                                                  '%25a'
         81        DO_FCALL                                          0  $55     
         82        ADD                                                  ~56     $55, 1
         83        QM_ASSIGN                                            ~54     ~56
         84    >   ASSIGN                                                       !9, ~54
   32    85        ROPE_INIT                                         3  ~59     'overlap+is+'
         86        ROPE_ADD                                          1  ~59     ~59, !9
         87        ROPE_END                                          2  ~58     ~59, '+days%0A'
         88        ECHO                                                         ~58
   33    89      > RETURN                                                       null

End of function range_overlap

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
170 ms | 2143 KiB | 20 Q