title: How to get previous month and year relative to today, using strtotime and date?
body: <p>I need to get previous month and year, relative to current date.</p>
  
  <p>However, see following example.</p>
  
  <pre><code>// Today is 2011-03-30
  echo date('Y-m-d', strtotime('last month'));
  
  // Output:
  2011-03-02
  </code></pre>
  
  <p>This behavior is understandable (to a certain point), due to different number of days in february and march, and code in example above is what I need, but works only 100% correctly for between 1st and 28th of each month.</p>
  
  <p>So, how to get last month AND year (think of <code>date("Y-m")</code>) in the most elegant manner as possible, which works for every day of the year? Optimal solution will be based on <code>strtotime</code> argument parsing.</p>
  
  <p>Update. To clarify requirements a bit.</p>
  
  <p>I have a piece of code that gets some statistics of last couple of months, but I first show stats from last month, and then load other months when needed. That's intended purpose. So, during THIS month, I want to find out which month-year should I pull in order to load PREVIOUS month stats.</p>
  
  <p>I also have a code that is timezone-aware (not really important right now), and that accepts <code>strtotime</code>-compatible string as input (to initialize internal date), and then allows date/time to be adjusted, also using <code>strtotime</code>-compatible strings.</p>
  
  <p>I know it can be done with few conditionals and basic math, but that's really messy, compared to this, for example (if it worked correctly, of course):</p>
  
  <pre><code>echo tz::date('last month')-&gt;format('Y-d')
  </code></pre>
  
  <p>So, I ONLY need previous month and year, in a <code>strtotime</code>-compatible fashion.</p>
  
  <p>Answer (thanks, @dnagirl):</p>
  
  <pre><code>// Today is 2011-03-30
  echo date('Y-m-d', strtotime('first day of last month')); // Output: 2011-02-01
  </code></pre>