title: Algorithm for calculating a series of future dates based on a frequency
body: <p>I'm working on a payment scheduler which plans out <code>n</code> payments based on an arbitrary start date and one of a set of frequencies (daily, weekly, monthly, etc...) and am seeking a general purpose algorithm for doing so.</p>
  
  <p>I have attempted a brute force means of doing this, by casing the frequency and adding a certain number of days, weeks, months as needed. This works for most purposes.</p>
  
  <p>Where it fails is when the arbitrary start date is after the 28th of a month and the frequency is somewhere between monthly and annually, especially for frequencies like 'first of each month' and 'last of each month'. Because days 29, 30, and 31 do not appear on all months, adding a month like <code>date('2013-10-31')-&gt;addMonth(1)</code> has unpredictable results. As does adding months like <code>date('2014-01-31')-&gt;addDays(30)</code>, again, due to February being unnecessarily short.</p>
  
  <p>Is there a general solution to this problem without the hideously complex cases I need for moving any given frequency through any given month?</p>
  
  <p>Bonus points for PHP, but I can translate if needed.</p>