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

End of function nextmonth

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
157.27 ms | 1406 KiB | 17 Q