3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * Return the next month date given a date and month increment * * 2010-01-31 +1 month = 2010-02-28 not 2010-03-03 * * @todo Need to do weekdays and holiday accounting * @param string $date Date to base from in Y-m-d format * @param integer $increment Number of months to increment * @return string Date after 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 $thirtyFirstOfDateMonthNum = \DateTime::createFromFormat('Y-m-d', $date)->modify('last day of this month')->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->add(new \DateInterval('P' . (int) $increment . 'M'))->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->add(new \DateInterval('P' . (int) $increment . 'M')); // 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/MpXqn
function name:  (null)
number of ops:  43
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   49     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'
   50     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'
   51    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'
   52    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'
   53    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'
   54    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'
   55    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 = 62, Position 2 = 64
Branch analysis from position: 62
2 jumps found. (Code = 43) Position 1 = 65, Position 2 = 66
Branch analysis from position: 65
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 66
Branch analysis from position: 64
filename:       /in/MpXqn
function name:  nextMonth
number of ops:  72
compiled vars:  !0 = $date, !1 = $increment, !2 = $firstOfDateMonth, !3 = $thirtyFirstOfDateMonthNum, !4 = $thirtyFirstOfDateMonth, !5 = $thirtyFirstOfNextMonth, !6 = $thirtyFirstOfNextMonthNum, !7 = $dayOfDateMonth, !8 = $dayOfDateMonthNum, !9 = $dt
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   13     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   16     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
   18    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        INIT_METHOD_CALL                                         $14, 'format'
         18        SEND_VAL_EX                                              'd'
         19        DO_FCALL                                      0  $15     
         20        ASSIGN                                                   !3, $15
   19    21        UNSET_CV                                                 !4
   22    22        ASSIGN                                                   !5, !2
   23    23        UNSET_CV                                                 !2
   24    24        INIT_METHOD_CALL                                         !5, 'add'
         25        NEW                                              $18     'DateInterval'
         26        CAST                                          4  ~19     !1
         27        CONCAT                                           ~20     'P', ~19
         28        CONCAT                                           ~21     ~20, 'M'
         29        SEND_VAL_EX                                              ~21
         30        DO_FCALL                                      0          
         31        SEND_VAR_NO_REF_EX                                       $18
         32        DO_FCALL                                      0  $23     
         33        INIT_METHOD_CALL                                         $23, 'modify'
         34        SEND_VAL_EX                                              'last+day+of+this+month'
         35        DO_FCALL                                      0          
   26    36        INIT_METHOD_CALL                                         !5, 'format'
         37        SEND_VAL_EX                                              'd'
         38        DO_FCALL                                      0  $25     
         39        ASSIGN                                                   !6, $25
   29    40        INIT_STATIC_METHOD_CALL                                  'DateTime', 'createFromFormat'
         41        SEND_VAL                                                 'Y-m-d'
         42        SEND_VAR                                                 !0
         43        DO_FCALL                                      0  $27     
         44        ASSIGN                                                   !7, $27
   31    45        INIT_METHOD_CALL                                         !7, 'format'
         46        SEND_VAL_EX                                              'd'
         47        DO_FCALL                                      0  $29     
         48        ASSIGN                                                   !8, $29
   34    49        ASSIGN                                                   !9, !7
   35    50        UNSET_CV                                                 !7
   36    51        INIT_METHOD_CALL                                         !9, 'add'
         52        NEW                                              $32     'DateInterval'
         53        CAST                                          4  ~33     !1
         54        CONCAT                                           ~34     'P', ~33
         55        CONCAT                                           ~35     ~34, 'M'
         56        SEND_VAL_EX                                              ~35
         57        DO_FCALL                                      0          
         58        SEND_VAR_NO_REF_EX                                       $32
         59        DO_FCALL                                      0          
   39    60        IS_SMALLER                                       ~38     !6, !3
         61      > JMPZ_EX                                          ~38     ~38, ->64
         62    >   IS_SMALLER                                       ~39     !6, !8
         63        BOOL                                             ~38     ~39
         64    > > JMPZ                                                     ~38, ->66
   41    65    >   ASSIGN                                                   !9, !5
   44    66    >   UNSET_CV                                                 !5
   46    67        INIT_METHOD_CALL                                         !9, 'format'
         68        SEND_VAL_EX                                              'Y-m-d'
         69        DO_FCALL                                      0  $41     
         70      > RETURN                                                   $41
   47    71*     > RETURN                                                   null

End of function nextmonth

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
161.19 ms | 1407 KiB | 20 Q