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'); $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/T16PB
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 = 59, Position 2 = 61
Branch analysis from position: 59
2 jumps found. (Code = 43) Position 1 = 62, Position 2 = 71
Branch analysis from position: 62
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 71
Branch analysis from position: 61
filename:       /in/T16PB
function name:  nextMonth
number of ops:  76
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-d'
          4        SEND_VAR                                                 !0
          5        DO_FCALL                                      0  $7      
          6        INIT_METHOD_CALL                                         $7, 'modify'
          7        SEND_VAL_EX                                              'first+day+of+this+month'
          8        DO_FCALL                                      0  $8      
          9        ASSIGN                                                   !2, $8
    6    10        INIT_STATIC_METHOD_CALL                                  'DateTime', 'createFromFormat'
         11        SEND_VAL                                                 'Y-m-d'
         12        SEND_VAR                                                 !0
         13        DO_FCALL                                      0  $10     
         14        INIT_METHOD_CALL                                         $10, 'modify'
         15        SEND_VAL_EX                                              'last+day+of+this+month'
         16        DO_FCALL                                      0  $11     
         17        ASSIGN                                                   !3, $11
    7    18        INIT_METHOD_CALL                                         !3, 'format'
         19        SEND_VAL_EX                                              'd'
         20        DO_FCALL                                      0  $13     
         21        ASSIGN                                                   !3, $13
    9    22        CLONE                                            ~15     !2
         23        ASSIGN                                                   !4, ~15
   10    24        INIT_METHOD_CALL                                         !4, 'modify'
         25        CAST                                          4  ~17     !1
         26        CONCAT                                           ~18     '%2B', ~17
         27        CONCAT                                           ~19     ~18, '+month'
         28        SEND_VAL_EX                                              ~19
         29        DO_FCALL                                      0  $20     
         30        INIT_METHOD_CALL                                         $20, 'modify'
         31        SEND_VAL_EX                                              'last+day+of+this+month'
         32        DO_FCALL                                      0          
   11    33        INIT_METHOD_CALL                                         !4, 'format'
         34        SEND_VAL_EX                                              'd'
         35        DO_FCALL                                      0  $22     
         36        ASSIGN                                                   !4, $22
   13    37        INIT_STATIC_METHOD_CALL                                  'DateTime', 'createFromFormat'
         38        SEND_VAL                                                 'Y-m-d'
         39        SEND_VAR                                                 !0
         40        DO_FCALL                                      0  $24     
         41        ASSIGN                                                   !5, $24
   14    42        INIT_METHOD_CALL                                         !5, 'format'
         43        SEND_VAL_EX                                              'd'
         44        DO_FCALL                                      0  $26     
         45        ASSIGN                                                   !5, $26
   16    46        INIT_STATIC_METHOD_CALL                                  'DateTime', 'createFromFormat'
         47        SEND_VAL                                                 'Y-m-d'
         48        SEND_VAR                                                 !0
         49        DO_FCALL                                      0  $28     
         50        ASSIGN                                                   !6, $28
   17    51        INIT_METHOD_CALL                                         !6, 'modify'
         52        CAST                                          4  ~30     !1
         53        CONCAT                                           ~31     '%2B', ~30
         54        CONCAT                                           ~32     ~31, '+month'
         55        SEND_VAL_EX                                              ~32
         56        DO_FCALL                                      0          
   18    57        IS_SMALLER                                       ~34     !4, !3
         58      > JMPZ_EX                                          ~34     ~34, ->61
         59    >   IS_SMALLER                                       ~35     !4, !5
         60        BOOL                                             ~34     ~35
         61    > > JMPZ                                                     ~34, ->71
   19    62    >   CLONE                                            ~36     !2
         63        ASSIGN                                                   !6, ~36
   20    64        INIT_METHOD_CALL                                         !6, 'modify'
         65        CAST                                          4  ~38     !1
         66        CONCAT                                           ~39     '%2B', ~38
         67        CONCAT                                           ~40     ~39, '+month+%2B'
         68        CONCAT                                           ~41     ~40, !4
         69        SEND_VAL_EX                                              ~41
         70        DO_FCALL                                      0          
   23    71    >   INIT_METHOD_CALL                                         !6, 'format'
         72        SEND_VAL_EX                                              'Y-m-d'
         73        DO_FCALL                                      0  $43     
         74      > RETURN                                                   $43
   24    75*     > RETURN                                                   null

End of function nextmonth

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
152.6 ms | 1406 KiB | 17 Q