3v4l.org

run code in 500+ PHP versions simultaneously
<?php function dates_in_consecutive_range(string $start_date, string $end_date, array $dates_list): bool { // If either $start_date or $end_date or both is not in $dates_list, return false if (!in_array($start_date, $dates_list) || !in_array($end_date, $dates_list)) { return false; } // If in consecutive range without missing days (holes), return true, else return false $start_dt = new DateTime($start_date); $end_dt = new DateTime($end_date); $interval = DateInterval::createFromDateString('1 day'); $period = new DatePeriod($start_dt, $interval, $end_dt); foreach ($period as $dt) { $curr_date = $dt->format("Y-m-d"); if (!in_array($curr_date, $dates_list)) { return false; } } return true; } $dates_list = [ '2022-03-11', '2022-03-12', '2022-03-13', '2022-03-14', '2022-03-18', '2022-03-19', ]; $start_date = '2022-03-11'; $end_date = '2022-03-14'; var_dump(dates_in_consecutive_range($start_date, $end_date, $dates_list)); // true $start_date = '2022-03-11'; $end_date = '2022-03-18'; var_dump(dates_in_consecutive_range($start_date, $end_date, $dates_list)); // false $start_date = '2022-03-18'; $end_date = '2022-03-19'; var_dump(dates_in_consecutive_range($start_date, $end_date, $dates_list)); // true $start_date = '2022-03-19'; $end_date = '2022-03-20'; var_dump(dates_in_consecutive_range($start_date, $end_date, $dates_list)); // false ($end_date not in $dates_list)
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/6FcL2
function name:  (null)
number of ops:  42
compiled vars:  !0 = $dates_list, !1 = $start_date, !2 = $end_date
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   26     0  E >   ASSIGN                                                       !0, <array>
   35     1        ASSIGN                                                       !1, '2022-03-11'
   36     2        ASSIGN                                                       !2, '2022-03-14'
   38     3        INIT_FCALL                                                   'var_dump'
          4        INIT_FCALL                                                   'dates_in_consecutive_range'
          5        SEND_VAR                                                     !1
          6        SEND_VAR                                                     !2
          7        SEND_VAR                                                     !0
          8        DO_FCALL                                          0  $6      
          9        SEND_VAR                                                     $6
         10        DO_ICALL                                                     
   40    11        ASSIGN                                                       !1, '2022-03-11'
   41    12        ASSIGN                                                       !2, '2022-03-18'
   43    13        INIT_FCALL                                                   'var_dump'
         14        INIT_FCALL                                                   'dates_in_consecutive_range'
         15        SEND_VAR                                                     !1
         16        SEND_VAR                                                     !2
         17        SEND_VAR                                                     !0
         18        DO_FCALL                                          0  $10     
         19        SEND_VAR                                                     $10
         20        DO_ICALL                                                     
   45    21        ASSIGN                                                       !1, '2022-03-18'
   46    22        ASSIGN                                                       !2, '2022-03-19'
   48    23        INIT_FCALL                                                   'var_dump'
         24        INIT_FCALL                                                   'dates_in_consecutive_range'
         25        SEND_VAR                                                     !1
         26        SEND_VAR                                                     !2
         27        SEND_VAR                                                     !0
         28        DO_FCALL                                          0  $14     
         29        SEND_VAR                                                     $14
         30        DO_ICALL                                                     
   50    31        ASSIGN                                                       !1, '2022-03-19'
   51    32        ASSIGN                                                       !2, '2022-03-20'
   53    33        INIT_FCALL                                                   'var_dump'
         34        INIT_FCALL                                                   'dates_in_consecutive_range'
         35        SEND_VAR                                                     !1
         36        SEND_VAR                                                     !2
         37        SEND_VAR                                                     !0
         38        DO_FCALL                                          0  $18     
         39        SEND_VAR                                                     $18
         40        DO_ICALL                                                     
         41      > RETURN                                                       1

Function dates_in_consecutive_range:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 47) Position 1 = 6, Position 2 = 9
Branch analysis from position: 6
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 11
Branch analysis from position: 10
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 11
2 jumps found. (Code = 77) Position 1 = 30, Position 2 = 41
Branch analysis from position: 30
2 jumps found. (Code = 78) Position 1 = 31, Position 2 = 41
Branch analysis from position: 31
2 jumps found. (Code = 43) Position 1 = 38, Position 2 = 40
Branch analysis from position: 38
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 40
1 jumps found. (Code = 42) Position 1 = 30
Branch analysis from position: 30
Branch analysis from position: 41
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 41
Branch analysis from position: 9
filename:       /in/6FcL2
function name:  dates_in_consecutive_range
number of ops:  45
compiled vars:  !0 = $start_date, !1 = $end_date, !2 = $dates_list, !3 = $start_dt, !4 = $end_dt, !5 = $interval, !6 = $period, !7 = $dt, !8 = $curr_date
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    3     0  E >   RECV                                                 !0      
          1        RECV                                                 !1      
          2        RECV                                                 !2      
    5     3        FRAMELESS_ICALL_2                in_array            ~9      !0, !2
          4        BOOL_NOT                                             ~10     ~9
          5      > JMPNZ_EX                                             ~10     ~10, ->9
          6    >   FRAMELESS_ICALL_2                in_array            ~11     !1, !2
          7        BOOL_NOT                                             ~12     ~11
          8        BOOL                                                 ~10     ~12
          9    > > JMPZ                                                         ~10, ->11
    6    10    > > RETURN                                                       <false>
   10    11    >   NEW                                                  $13     'DateTime'
         12        SEND_VAR_EX                                                  !0
         13        DO_FCALL                                          0          
         14        ASSIGN                                                       !3, $13
   11    15        NEW                                                  $16     'DateTime'
         16        SEND_VAR_EX                                                  !1
         17        DO_FCALL                                          0          
         18        ASSIGN                                                       !4, $16
   13    19        INIT_STATIC_METHOD_CALL                                      'DateInterval', 'createFromDateString'
         20        SEND_VAL                                                     '1+day'
         21        DO_FCALL                                          0  $19     
         22        ASSIGN                                                       !5, $19
   14    23        NEW                                                  $21     'DatePeriod'
         24        SEND_VAR_EX                                                  !3
         25        SEND_VAR_EX                                                  !5
         26        SEND_VAR_EX                                                  !4
         27        DO_FCALL                                          0          
         28        ASSIGN                                                       !6, $21
   16    29      > FE_RESET_R                                           $24     !6, ->41
         30    > > FE_FETCH_R                                                   $24, !7, ->41
   17    31    >   INIT_METHOD_CALL                                             !7, 'format'
         32        SEND_VAL_EX                                                  'Y-m-d'
         33        DO_FCALL                                          0  $25     
         34        ASSIGN                                                       !8, $25
   18    35        FRAMELESS_ICALL_2                in_array            ~27     !8, !2
         36        BOOL_NOT                                             ~28     ~27
         37      > JMPZ                                                         ~28, ->40
   19    38    >   FE_FREE                                                      $24
         39      > RETURN                                                       <false>
   16    40    > > JMP                                                          ->30
         41    >   FE_FREE                                                      $24
   23    42      > RETURN                                                       <true>
   24    43*       VERIFY_RETURN_TYPE                                           
         44*     > RETURN                                                       null

End of function dates_in_consecutive_range

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
139.23 ms | 1493 KiB | 18 Q