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

End of function nextmonth

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
151.15 ms | 1407 KiB | 20 Q