3v4l.org

run code in 300+ PHP versions simultaneously
<?php function nextMonth($date, $increment) { $firstOfDateMonth = \DateTime::createFromFormat('Y-m-01', $date); $thirtyFirstOfDateMonth = \DateTime::createFromFormat('Y-m-d', $date)->modify('last day of this month'); $thirtyFirstOfDateMonth = $thirtyFirstOfDateMonth->format('d'); $thirtyFirstOfNextMonth = clone $firstOfDateMonth; $thirtyFirstOfNextMonth->modify('+' . (int) $increment . ' month')->modify('last day of this month'); $thirtyFirstOfNextMonth = $thirtyFirstOfNextMonth->format('d'); $dayOfDateMonth = \DateTime::createFromFormat('Y-m-d', $date); $dayOfDateMonth = $dayOfDateMonth->format('d'); $dt = \DateTime::createFromFormat('Y-m-d', $date); $dt->modify('+' . (int) $increment . ' month'); if ($thirtyFirstOfNextMonth < $thirtyFirstOfDateMonth && $thirtyFirstOfNextMonth < $dayOfDateMonth) { $dt = clone $firstOfDateMonth; $dt->modify('+' . (int) $increment . ' month +' . $thirtyFirstOfNextMonth); } return $dt->format('Y-m-d'); } echo nextMonth('2009-12-31', 2),"\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/gfbn2
function name:  (null)
number of ops:  25
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   25     0  E >   INIT_FCALL                                               'nextmonth'
          1        SEND_VAL                                                 '2009-12-31'
          2        SEND_VAL                                                 2
          3        DO_FCALL                                      0  $0      
          4        ECHO                                                     $0
          5        ECHO                                                     '%0A'
   26     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'
   27    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'
   28    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 = 56, Position 2 = 58
Branch analysis from position: 56
2 jumps found. (Code = 43) Position 1 = 59, Position 2 = 68
Branch analysis from position: 59
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 68
Branch analysis from position: 58
filename:       /in/gfbn2
function name:  nextMonth
number of ops:  73
compiled vars:  !0 = $date, !1 = $increment, !2 = $firstOfDateMonth, !3 = $thirtyFirstOfDateMonth, !4 = $thirtyFirstOfNextMonth, !5 = $dayOfDateMonth, !6 = $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-01'
          4        SEND_VAR                                                 !0
          5        DO_FCALL                                      0  $7      
          6        ASSIGN                                                   !2, $7
    6     7        INIT_STATIC_METHOD_CALL                                  'DateTime', 'createFromFormat'
          8        SEND_VAL                                                 'Y-m-d'
          9        SEND_VAR                                                 !0
         10        DO_FCALL                                      0  $9      
         11        INIT_METHOD_CALL                                         $9, 'modify'
         12        SEND_VAL_EX                                              'last+day+of+this+month'
         13        DO_FCALL                                      0  $10     
         14        ASSIGN                                                   !3, $10
    7    15        INIT_METHOD_CALL                                         !3, 'format'
         16        SEND_VAL_EX                                              'd'
         17        DO_FCALL                                      0  $12     
         18        ASSIGN                                                   !3, $12
    9    19        CLONE                                            ~14     !2
         20        ASSIGN                                                   !4, ~14
   10    21        INIT_METHOD_CALL                                         !4, 'modify'
         22        CAST                                          4  ~16     !1
         23        CONCAT                                           ~17     '%2B', ~16
         24        CONCAT                                           ~18     ~17, '+month'
         25        SEND_VAL_EX                                              ~18
         26        DO_FCALL                                      0  $19     
         27        INIT_METHOD_CALL                                         $19, 'modify'
         28        SEND_VAL_EX                                              'last+day+of+this+month'
         29        DO_FCALL                                      0          
   11    30        INIT_METHOD_CALL                                         !4, 'format'
         31        SEND_VAL_EX                                              'd'
         32        DO_FCALL                                      0  $21     
         33        ASSIGN                                                   !4, $21
   13    34        INIT_STATIC_METHOD_CALL                                  'DateTime', 'createFromFormat'
         35        SEND_VAL                                                 'Y-m-d'
         36        SEND_VAR                                                 !0
         37        DO_FCALL                                      0  $23     
         38        ASSIGN                                                   !5, $23
   14    39        INIT_METHOD_CALL                                         !5, 'format'
         40        SEND_VAL_EX                                              'd'
         41        DO_FCALL                                      0  $25     
         42        ASSIGN                                                   !5, $25
   16    43        INIT_STATIC_METHOD_CALL                                  'DateTime', 'createFromFormat'
         44        SEND_VAL                                                 'Y-m-d'
         45        SEND_VAR                                                 !0
         46        DO_FCALL                                      0  $27     
         47        ASSIGN                                                   !6, $27
   17    48        INIT_METHOD_CALL                                         !6, 'modify'
         49        CAST                                          4  ~29     !1
         50        CONCAT                                           ~30     '%2B', ~29
         51        CONCAT                                           ~31     ~30, '+month'
         52        SEND_VAL_EX                                              ~31
         53        DO_FCALL                                      0          
   18    54        IS_SMALLER                                       ~33     !4, !3
         55      > JMPZ_EX                                          ~33     ~33, ->58
         56    >   IS_SMALLER                                       ~34     !4, !5
         57        BOOL                                             ~33     ~34
         58    > > JMPZ                                                     ~33, ->68
   19    59    >   CLONE                                            ~35     !2
         60        ASSIGN                                                   !6, ~35
   20    61        INIT_METHOD_CALL                                         !6, 'modify'
         62        CAST                                          4  ~37     !1
         63        CONCAT                                           ~38     '%2B', ~37
         64        CONCAT                                           ~39     ~38, '+month+%2B'
         65        CONCAT                                           ~40     ~39, !4
         66        SEND_VAL_EX                                              ~40
         67        DO_FCALL                                      0          
   23    68    >   INIT_METHOD_CALL                                         !6, 'format'
         69        SEND_VAL_EX                                              'Y-m-d'
         70        DO_FCALL                                      0  $42     
         71      > RETURN                                                   $42
   24    72*     > RETURN                                                   null

End of function nextmonth

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
152.33 ms | 1407 KiB | 17 Q