title: Convert date to current month date
body: <p>I need to convert any date to a date within 1 month from today. Dates that are smaller than today should be converted to next month. </p>
  
  <p>For example, current day is 2018-09-11 and dates are:</p>
  
  <pre><code>$dates = ['2018-08-01', '2018-09-11', '2018-09-15', '2018-10-15', '2018-12-31', '2019-01-01', '2019-01-31'];
  </code></pre>
  
  <p>Results should be as follows, respectively:</p>
  
  <pre><code>$result = ['2018-10-01', '2018-09-11', '2018-09-15', '2018-09-15', '2018-09-30', '2018-10-01, '2018-09-30'];
  </code></pre>
  
  <p>Edit: I have tried so far using strtotime('+1 month') but it fails on dates such as 2018-10-31 since it'll give 2018-12-01. It also fails on using strtotime('-1 month') on dates such as 2018-10-31 where it gives 2018-10-01 instead of 2018-09-30. </p>
  
  <p>My current solution is following, but I believe it can be done MUCH simpler somehow:  </p>
  
  <pre><code>$timestamp = strtotime('2018-10-31');
  if (date('d', $timestamp) &gt; date('t')) {
      $date = date('Y-m-t');
  } elseif(date('d', $timestamp) &lt; date('d')) {
      $year = date('Y');
      $month = date('m') == 12 ? 1 : (intval(date('n')) + 1);
      if ($month &lt; 10) {
          $month = '0'.$month;
      }
      $day = date('d', $timestamp);
      $date = $year . '-' . $month . '-' . $day;
  } else {
      $date = date('Y-m') . '-' . date('d', $timestamp);
  }
  </code></pre>