How to get previous month and year relative to today, using strtotime and date?

How to get previous month and year relative to today, using strtotime and date?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

The "Last Month" Edge Case in PHP Date Math

When working with temporal data in PHP, developers often encounter a subtle but persistent bug related to month boundaries. A common requirement is to retrieve statistics or records from the previous calendar month. While strtotime() is powerful for relative dates, its default behavior with the string 'last month' can yield surprising results near the end of a current month.

Consider a scenario where today is March 30th. If you execute date('Y-m-d', strtotime('last month')), PHP calculates the timestamp for the last month relative to the current day, not the start of that month. Because March has 31 days and February has 28 (or 29), the calculation rolls back from March 30th by approximately 30 or 31 days. This lands you on February 28th or 29th, depending on the leap year. While technically "last month" in a loose sense, it is not the start of February, which breaks logic that assumes Y-m formatting or full-month comparisons.

The solution lies in adjusting the reference point before applying the relative date modifier. Instead of asking PHP to find "last month" from today's specific day, you must first normalize today's date to the beginning of the current month. Once the date is anchored to the 1st of the current month, subtracting one month yields the exact first day of the previous month, regardless of the number of days in either month.

The most elegant implementation using strtotime involves a two-step string parsing process:

// Today is 2011-03-30
// Step 1: Get the first day of the current month (e.g., 2011-03-01)
$currentMonthStart = date('Y-m-d', strtotime('first day of this month'));

// Step 2: Subtract one month from that anchor point
$previousMonthStart = date('Y-m-d', strtotime('-1 month', strtotime($currentMonthStart)));

echo $previousMonthStart; // Output: 2011-02-01

Alternatively, you can chain the strtotime arguments directly to achieve the same result without an intermediate variable. By specifying 'first day of this month' as the base time and then passing a negative offset of one month, PHP handles the arithmetic correctly:

echo date('Y-m-d', strtotime('-1 month', strtotime('first day of this month')));
// Output: 2011-02-01

This approach works universally. Whether today is January 1st, December 31st, or any day in between, normalizing to the start of the current month eliminates the variance caused by differing month lengths. The resulting date represents the first day of the previous calendar month, allowing for consistent formatting like Y-m (e.g., 2011-02) without needing conditional logic to handle edge cases like February 28th versus March 30th.

For applications requiring timezone awareness or complex date adjustments, this pattern remains robust because it relies on the internal timestamp logic of PHP rather than manual day-counting arithmetic. It avoids the messiness of date('t') checks and subtraction loops, providing a clean, declarative solution that aligns with how strtotime is intended to be used for relative time calculations.

Developers building reporting dashboards or statistical aggregators can rely on this method to ensure that data fetched for "last month" always corresponds to the correct calendar period, preventing off-by-one errors that frequently plague financial or analytical reports.

Our network