3v4l.org

run code in 300+ PHP versions simultaneously
<?php function nextMonth($date, $increment) { $firstOfDateMonth = \DateTime::createFromFormat('Y-m-d', $date)->modify('first day of this month'); $thirtyFirstOfDateMonth = \DateTime::createFromFormat('Y-m-d', $date)->modify('last day of this month'); $thirtyFirstOfDateMonthNum = $thirtyFirstOfDateMonth->format('d'); $thirtyFirstOfNextMonth = clone $firstOfDateMonth; $thirtyFirstOfNextMonth->modify('+' . (int) $increment . ' month')->modify('last day of this month'); $thirtyFirstOfNextMonthNum = $thirtyFirstOfNextMonth->format('d'); $dayOfDateMonth = \DateTime::createFromFormat('Y-m-d', $date); $dayOfDateMonthNum = $dayOfDateMonth->format('d'); $dt = \DateTime::createFromFormat('Y-m-d', $date); $dt->modify('+' . (int) $increment . ' month'); if ($thirtyFirstOfNextMonthNum < $thirtyFirstOfDateMonthNum && $thirtyFirstOfNextMonthNum < $dayOfDateMonthNum) { $dt = clone $thirtyFirstOfNextMonth; } return $dt->format('Y-m-d'); } echo nextMonth('2009-12-31', 3),"\n"; echo nextMonth('2010-01-31', 1),"\n"; echo nextMonth('2010-01-05', 1),"\n"; echo nextMonth('2010-12-05', 1),"\n";
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/VsbUK
function name:  (null)
number of ops:  25
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   24     0  E >   INIT_FCALL                                               'nextmonth'
          1        SEND_VAL                                                 '2009-12-31'
          2        SEND_VAL                                                 3
          3        DO_FCALL                                      0  $0      
          4        ECHO                                                     $0
          5        ECHO                                                     '%0A'
   25     6        INIT_FCALL                                               'nextmonth'
          7        SEND_VAL                                                 '2010-01-31'
          8        SEND_VAL                                                 1
          9        DO_FCALL                                      0  $1      
         10        ECHO                                                     $1
         11        ECHO                                                     '%0A'
   26    12        INIT_FCALL                                               'nextmonth'
         13        SEND_VAL                                                 '2010-01-05'
         14        SEND_VAL                                                 1
         15        DO_FCALL                                      0  $2      
         16        ECHO                                                     $2
         17        ECHO                                                     '%0A'
   27    18        INIT_FCALL                                               'nextmonth'
         19        SEND_VAL                                                 '2010-12-05'
         20        SEND_VAL                                                 1
         21        DO_FCALL                                      0  $3      
         22        ECHO                                                     $3
         23        ECHO                                                     '%0A'
         24      > RETURN                                                   1

Function nextmonth:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 46) Position 1 = 59, Position 2 = 61
Branch analysis from position: 59
2 jumps found. (Code = 43) Position 1 = 62, Position 2 = 64
Branch analysis from position: 62
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 64
Branch analysis from position: 61
filename:       /in/VsbUK
function name:  nextMonth
number of ops:  69
compiled vars:  !0 = $date, !1 = $increment, !2 = $firstOfDateMonth, !3 = $thirtyFirstOfDateMonth, !4 = $thirtyFirstOfDateMonthNum, !5 = $thirtyFirstOfNextMonth, !6 = $thirtyFirstOfNextMonthNum, !7 = $dayOfDateMonth, !8 = $dayOfDateMonthNum, !9 = $dt
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
          1        RECV                                             !1      
    5     2        INIT_STATIC_METHOD_CALL                                  'DateTime', 'createFromFormat'
          3        SEND_VAL                                                 'Y-m-d'
          4        SEND_VAR                                                 !0
          5        DO_FCALL                                      0  $10     
          6        INIT_METHOD_CALL                                         $10, 'modify'
          7        SEND_VAL_EX                                              'first+day+of+this+month'
          8        DO_FCALL                                      0  $11     
          9        ASSIGN                                                   !2, $11
    6    10        INIT_STATIC_METHOD_CALL                                  'DateTime', 'createFromFormat'
         11        SEND_VAL                                                 'Y-m-d'
         12        SEND_VAR                                                 !0
         13        DO_FCALL                                      0  $13     
         14        INIT_METHOD_CALL                                         $13, 'modify'
         15        SEND_VAL_EX                                              'last+day+of+this+month'
         16        DO_FCALL                                      0  $14     
         17        ASSIGN                                                   !3, $14
    7    18        INIT_METHOD_CALL                                         !3, 'format'
         19        SEND_VAL_EX                                              'd'
         20        DO_FCALL                                      0  $16     
         21        ASSIGN                                                   !4, $16
    9    22        CLONE                                            ~18     !2
         23        ASSIGN                                                   !5, ~18
   10    24        INIT_METHOD_CALL                                         !5, 'modify'
         25        CAST                                          4  ~20     !1
         26        CONCAT                                           ~21     '%2B', ~20
         27        CONCAT                                           ~22     ~21, '+month'
         28        SEND_VAL_EX                                              ~22
         29        DO_FCALL                                      0  $23     
         30        INIT_METHOD_CALL                                         $23, 'modify'
         31        SEND_VAL_EX                                              'last+day+of+this+month'
         32        DO_FCALL                                      0          
   11    33        INIT_METHOD_CALL                                         !5, 'format'
         34        SEND_VAL_EX                                              'd'
         35        DO_FCALL                                      0  $25     
         36        ASSIGN                                                   !6, $25
   13    37        INIT_STATIC_METHOD_CALL                                  'DateTime', 'createFromFormat'
         38        SEND_VAL                                                 'Y-m-d'
         39        SEND_VAR                                                 !0
         40        DO_FCALL                                      0  $27     
         41        ASSIGN                                                   !7, $27
   14    42        INIT_METHOD_CALL                                         !7, 'format'
         43        SEND_VAL_EX                                              'd'
         44        DO_FCALL                                      0  $29     
         45        ASSIGN                                                   !8, $29
   16    46        INIT_STATIC_METHOD_CALL                                  'DateTime', 'createFromFormat'
         47        SEND_VAL                                                 'Y-m-d'
         48        SEND_VAR                                                 !0
         49        DO_FCALL                                      0  $31     
         50        ASSIGN                                                   !9, $31
   17    51        INIT_METHOD_CALL                                         !9, 'modify'
         52        CAST                                          4  ~33     !1
         53        CONCAT                                           ~34     '%2B', ~33
         54        CONCAT                                           ~35     ~34, '+month'
         55        SEND_VAL_EX                                              ~35
         56        DO_FCALL                                      0          
   18    57        IS_SMALLER                                       ~37     !6, !4
         58      > JMPZ_EX                                          ~37     ~37, ->61
         59    >   IS_SMALLER                                       ~38     !6, !8
         60        BOOL                                             ~37     ~38
         61    > > JMPZ                                                     ~37, ->64
   19    62    >   CLONE                                            ~39     !5
         63        ASSIGN                                                   !9, ~39
   22    64    >   INIT_METHOD_CALL                                         !9, 'format'
         65        SEND_VAL_EX                                              'Y-m-d'
         66        DO_FCALL                                      0  $41     
         67      > RETURN                                                   $41
   23    68*     > RETURN                                                   null

End of function nextmonth

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
152.56 ms | 1398 KiB | 17 Q