3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * Return the next month date given a date and month increment * * @param string $date Date to base from in Y-m-d format * @param integer $increment Number of months to increment */ function nextMonth($date, $increment) { // Grab the first day of month of the passed in date $firstOfDateMonth = \DateTime::createFromFormat('Y-m-d', $date)->modify('first day of this month'); // Grab the last day of month of the passed in date $thirtyFirstOfDateMonth = \DateTime::createFromFormat('Y-m-d', $date)->modify('last day of this month'); // Just get the number of the last day $thirtyFirstOfDateMonthNum = $thirtyFirstOfDateMonth->format('d'); unset($thirtyFirstOfDateMonth); // Using the first day of month of the passed in date add month increment and get last day of that month $thirtyFirstOfNextMonth = clone $firstOfDateMonth; unset($firstOfDateMonth); $thirtyFirstOfNextMonth->modify('+' . (int) $increment . ' month')->modify('last day of this month'); // Just get the number of the last day $thirtyFirstOfNextMonthNum = $thirtyFirstOfNextMonth->format('d'); // Turn the passed in date into a DateTime object $dayOfDateMonth = \DateTime::createFromFormat('Y-m-d', $date); // Just get the number of the day $dayOfDateMonthNum = $dayOfDateMonth->format('d'); // Using the passed in date add month increment and assume correct so far $dt = clone $dayOfDateMonth; unset($dayOfDateMonth); $dt->modify('+' . (int) $increment . ' month'); // If the last day of next month < last day of month from passed date && last day of next month < passed date day number if ($thirtyFirstOfNextMonthNum < $thirtyFirstOfDateMonthNum && $thirtyFirstOfNextMonthNum < $dayOfDateMonthNum) { // PHP has gone over the date we want so use last day of next month $dt = clone $thirtyFirstOfNextMonth; } unset($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"; echo nextMonth('2010-01-31', 13),"\n"; echo nextMonth('2010-01-31', 3),"\n"; echo nextMonth('2010-01-31', 0),"\n";
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/BLuhS
function name:  (null)
number of ops:  43
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   46     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'
   47     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'
   48    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'
   49    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'
   50    24        INIT_FCALL                                               'nextmonth'
         25        SEND_VAL                                                 '2010-01-31'
         26        SEND_VAL                                                 13
         27        DO_FCALL                                      0  $4      
         28        ECHO                                                     $4
         29        ECHO                                                     '%0A'
   51    30        INIT_FCALL                                               'nextmonth'
         31        SEND_VAL                                                 '2010-01-31'
         32        SEND_VAL                                                 3
         33        DO_FCALL                                      0  $5      
         34        ECHO                                                     $5
         35        ECHO                                                     '%0A'
   52    36        INIT_FCALL                                               'nextmonth'
         37        SEND_VAL                                                 '2010-01-31'
         38        SEND_VAL                                                 0
         39        DO_FCALL                                      0  $6      
         40        ECHO                                                     $6
         41        ECHO                                                     '%0A'
         42      > 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/BLuhS
function name:  nextMonth
number of ops:  70
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
-------------------------------------------------------------------------------------
    9     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   12     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
   14    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
   16    18        INIT_METHOD_CALL                                         !3, 'format'
         19        SEND_VAL_EX                                              'd'
         20        DO_FCALL                                      0  $16     
         21        ASSIGN                                                   !4, $16
   17    22        UNSET_CV                                                 !3
   20    23        CLONE                                            ~18     !2
         24        ASSIGN                                                   !5, ~18
   21    25        UNSET_CV                                                 !2
   22    26        INIT_METHOD_CALL                                         !5, 'modify'
         27        CAST                                          4  ~20     !1
         28        CONCAT                                           ~21     '%2B', ~20
         29        CONCAT                                           ~22     ~21, '+month'
         30        SEND_VAL_EX                                              ~22
         31        DO_FCALL                                      0  $23     
         32        INIT_METHOD_CALL                                         $23, 'modify'
         33        SEND_VAL_EX                                              'last+day+of+this+month'
         34        DO_FCALL                                      0          
   24    35        INIT_METHOD_CALL                                         !5, 'format'
         36        SEND_VAL_EX                                              'd'
         37        DO_FCALL                                      0  $25     
         38        ASSIGN                                                   !6, $25
   27    39        INIT_STATIC_METHOD_CALL                                  'DateTime', 'createFromFormat'
         40        SEND_VAL                                                 'Y-m-d'
         41        SEND_VAR                                                 !0
         42        DO_FCALL                                      0  $27     
         43        ASSIGN                                                   !7, $27
   29    44        INIT_METHOD_CALL                                         !7, 'format'
         45        SEND_VAL_EX                                              'd'
         46        DO_FCALL                                      0  $29     
         47        ASSIGN                                                   !8, $29
   32    48        CLONE                                            ~31     !7
         49        ASSIGN                                                   !9, ~31
   33    50        UNSET_CV                                                 !7
   34    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          
   36    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
   38    62    >   CLONE                                            ~39     !5
         63        ASSIGN                                                   !9, ~39
   41    64    >   UNSET_CV                                                 !5
   43    65        INIT_METHOD_CALL                                         !9, 'format'
         66        SEND_VAL_EX                                              'Y-m-d'
         67        DO_FCALL                                      0  $41     
         68      > RETURN                                                   $41
   44    69*     > RETURN                                                   null

End of function nextmonth

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
155.44 ms | 1398 KiB | 20 Q