3v4l.org

run code in 300+ 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 = 27
Branch analysis from position: 16
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 27
2 jumps found. (Code = 43) Position 1 = 35, Position 2 = 41
Branch analysis from position: 35
1 jumps found. (Code = 42) Position 1 = 55
Branch analysis from position: 55
2 jumps found. (Code = 44) Position 1 = 56, Position 2 = 42
Branch analysis from position: 56
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 42
2 jumps found. (Code = 43) Position 1 = 54, Position 2 = 55
Branch analysis from position: 54
2 jumps found. (Code = 44) Position 1 = 56, Position 2 = 42
Branch analysis from position: 56
Branch analysis from position: 42
Branch analysis from position: 55
Branch analysis from position: 41
Branch analysis from position: 15
filename:       /in/AZX0E
function name:  addDaysWithout
number of ops:  60
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, ->27
   18    16    >   INIT_FCALL                                               'implode'
         17        SEND_VAL                                                 '%2C'
         18        SEND_VAR                                                 !2
         19        DO_ICALL                                         $15     
         20        CONCAT                                           ~16     'Invalid+Argument+%22', $15
         21        CONCAT                                           ~17     ~16, '%22+in+withoutDays'
         22        ASSIGN                                                   !5, ~17
   19    23        NEW                                              $19     'InvalidArgumentException'
         24        SEND_VAR_EX                                              !5
         25        DO_FCALL                                      0          
         26      > THROW                                         0          $19
   21    27    >   CLONE                                            ~21     !0
         28        ASSIGN                                                   !6, ~21
   22    29        DIV                                              ~23     !1, !3
         30        CAST                                          4  ~24     ~23
         31        SUB                                              ~25     ~24, 1
         32        ASSIGN                                                   !7, ~25
   23    33        IS_SMALLER                                               0, !7
         34      > JMPZ                                                     ~27, ->41
   24    35    >   INIT_METHOD_CALL                                         !6, 'modify'
         36        CONCAT                                           ~28     !7, '+weeks'
         37        SEND_VAL_EX                                              ~28
         38        DO_FCALL                                      0          
   25    39        MUL                                              ~30     !7, !3
         40        ASSIGN_OP                                     2          !1, ~30
   27    41    > > JMP                                                      ->55
   28    42    >   INIT_METHOD_CALL                                         !6, 'modify'
         43        SEND_VAL_EX                                              '%2B1+Day'
         44        DO_FCALL                                      0          
   29    45        INIT_FCALL                                               'in_array'
         46        INIT_METHOD_CALL                                         !6, 'format'
         47        SEND_VAL_EX                                              'D'
         48        DO_FCALL                                      0  $33     
         49        SEND_VAR                                                 $33
         50        SEND_VAR                                                 !2
         51        DO_ICALL                                         $34     
         52        BOOL_NOT                                         ~35     $34
         53      > JMPZ                                                     ~35, ->55
   30    54    >   PRE_DEC                                                  !1
   27    55    > > JMPNZ                                                    !1, ->42
   33    56    >   VERIFY_RETURN_TYPE                                       !6
         57      > RETURN                                                   !6
   34    58*       VERIFY_RETURN_TYPE                                       
         59*     > RETURN                                                   null

End of function adddayswithout

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
147.48 ms | 1017 KiB | 20 Q