3v4l.org

run code in 500+ PHP versions simultaneously
<?php /** * Add days without specific days of the week * * @param DateTime $startDate start Date * @param int $days number of days * @param array $withoutDays array with elements "Mon", "Tue", "Wed", "Thu", "Fri", "Sat","Sun" * @return DateTime */ function addDaysWithout(DateTime $startDate ,int $days, array $withoutDays = []) : DateTime { //validate $withoutDays $validWeekDays = 7 - count($withoutDays); $validIdentifiers = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat","Sun"]; if($validWeekDays <= 0 OR $withoutDays != array_intersect($withoutDays,$validIdentifiers)){ $msg = 'Invalid Argument "'.implode(',',$withoutDays).'" in withoutDays'; throw new \InvalidArgumentException($msg); } $start = clone $startDate; $fullWeeks = (int)($days/$validWeekDays)-1; if($fullWeeks > 0){ $start ->modify($fullWeeks.' weeks'); $days -= $fullWeeks * $validWeekDays; } while($days){ $start->modify('+1 Day'); if(!in_array($start->format('D'),$withoutDays)){ --$days; } } return $start; } $start = date_create('2022-09-05'); $dt = addDaysWithout($start,30,["Sun"]); var_dump($dt); //object(DateTime)#3 (3) { ["date"]=> string(26) "2022-10-10 00:00:00.000000" $start = date_create('2022-09-05'); $dt = addDaysWithout($start,30,["Mon","Sun"]); var_dump($dt); //object(DateTime)#3 (3) { ["date"]=> string(26) "2022-10-15
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/AZX0E
function name:  (null)
number of ops:  27
compiled vars:  !0 = $start, !1 = $dt
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   36     0  E >   INIT_FCALL                                                   'date_create'
          1        SEND_VAL                                                     '2022-09-05'
          2        DO_ICALL                                             $2      
          3        ASSIGN                                                       !0, $2
   37     4        INIT_FCALL                                                   'adddayswithout'
          5        SEND_VAR                                                     !0
          6        SEND_VAL                                                     30
          7        SEND_VAL                                                     <array>
          8        DO_FCALL                                          0  $4      
          9        ASSIGN                                                       !1, $4
   38    10        INIT_FCALL                                                   'var_dump'
         11        SEND_VAR                                                     !1
         12        DO_ICALL                                                     
   41    13        INIT_FCALL                                                   'date_create'
         14        SEND_VAL                                                     '2022-09-05'
         15        DO_ICALL                                             $7      
         16        ASSIGN                                                       !0, $7
   42    17        INIT_FCALL                                                   'adddayswithout'
         18        SEND_VAR                                                     !0
         19        SEND_VAL                                                     30
         20        SEND_VAL                                                     <array>
         21        DO_FCALL                                          0  $9      
         22        ASSIGN                                                       !1, $9
   43    23        INIT_FCALL                                                   'var_dump'
         24        SEND_VAR                                                     !1
         25        DO_ICALL                                                     
   44    26      > RETURN                                                       1

Function adddayswithout:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 47) Position 1 = 9, Position 2 = 15
Branch analysis from position: 9
2 jumps found. (Code = 43) Position 1 = 16, Position 2 = 24
Branch analysis from position: 16
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 24
2 jumps found. (Code = 43) Position 1 = 32, Position 2 = 38
Branch analysis from position: 32
1 jumps found. (Code = 42) Position 1 = 49
Branch analysis from position: 49
2 jumps found. (Code = 44) Position 1 = 50, Position 2 = 39
Branch analysis from position: 50
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 39
2 jumps found. (Code = 43) Position 1 = 48, Position 2 = 49
Branch analysis from position: 48
2 jumps found. (Code = 44) Position 1 = 50, Position 2 = 39
Branch analysis from position: 50
Branch analysis from position: 39
Branch analysis from position: 49
Branch analysis from position: 38
Branch analysis from position: 15
filename:       /in/AZX0E
function name:  addDaysWithout
number of ops:  54
compiled vars:  !0 = $startDate, !1 = $days, !2 = $withoutDays, !3 = $validWeekDays, !4 = $validIdentifiers, !5 = $msg, !6 = $start, !7 = $fullWeeks
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   11     0  E >   RECV                                                 !0      
          1        RECV                                                 !1      
          2        RECV_INIT                                            !2      <array>
   14     3        COUNT                                                ~8      !2
          4        SUB                                                  ~9      7, ~8
          5        ASSIGN                                                       !3, ~9
   15     6        ASSIGN                                                       !4, <array>
   16     7        IS_SMALLER_OR_EQUAL                                  ~12     !3, 0
          8      > JMPNZ_EX                                             ~12     ~12, ->15
   17     9    >   INIT_FCALL                                                   'array_intersect'
         10        SEND_VAR                                                     !2
         11        SEND_VAR                                                     !4
         12        DO_ICALL                                             $13     
         13        IS_NOT_EQUAL                                         ~14     !2, $13
         14        BOOL                                                 ~12     ~14
         15    > > JMPZ                                                         ~12, ->24
   18    16    >   FRAMELESS_ICALL_2                implode             ~15     '%2C', !2
         17        CONCAT                                               ~16     'Invalid+Argument+%22', ~15
         18        CONCAT                                               ~17     ~16, '%22+in+withoutDays'
         19        ASSIGN                                                       !5, ~17
   19    20        NEW                                                  $19     'InvalidArgumentException'
         21        SEND_VAR_EX                                                  !5
         22        DO_FCALL                                          0          
         23      > THROW                                             0          $19
   21    24    >   CLONE                                                ~21     !0
         25        ASSIGN                                                       !6, ~21
   22    26        DIV                                                  ~23     !1, !3
         27        CAST                                              4  ~24     ~23
         28        SUB                                                  ~25     ~24, 1
         29        ASSIGN                                                       !7, ~25
   23    30        IS_SMALLER                                                   0, !7
         31      > JMPZ                                                         ~27, ->38
   24    32    >   INIT_METHOD_CALL                                             !6, 'modify'
         33        CONCAT                                               ~28     !7, '+weeks'
         34        SEND_VAL_EX                                                  ~28
         35        DO_FCALL                                          0          
   25    36        MUL                                                  ~30     !7, !3
         37        ASSIGN_OP                                         2          !1, ~30
   27    38    > > JMP                                                          ->49
   28    39    >   INIT_METHOD_CALL                                             !6, 'modify'
         40        SEND_VAL_EX                                                  '%2B1+Day'
         41        DO_FCALL                                          0          
   29    42        INIT_METHOD_CALL                                             !6, 'format'
         43        SEND_VAL_EX                                                  'D'
         44        DO_FCALL                                          0  $33     
         45        FRAMELESS_ICALL_2                in_array            ~34     $33, !2
         46        BOOL_NOT                                             ~35     ~34
         47      > JMPZ                                                         ~35, ->49
   30    48    >   PRE_DEC                                                      !1
   27    49    > > JMPNZ                                                        !1, ->39
   33    50    >   VERIFY_RETURN_TYPE                                           !6
         51      > RETURN                                                       !6
   34    52*       VERIFY_RETURN_TYPE                                           
         53*     > RETURN                                                       null

End of function adddayswithout

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
220.81 ms | 1928 KiB | 18 Q